I'm trying to hash a string into an integer for placing it in an array. However I do not know all too much about hashing functions, and that's why my current method is just adding all the ASCII numbers of the characters together and taking it mod the array size.
Are there any simple faster/better methods?
I've tried many fast hash functions and chosen this one:
It is as fast as K&R function (actually even faster) but makes better (more even) distribution.
A very simple method is to just XOR all values. The simplest as far as I know.
As Dummy00001 pointed out, this has been asked and answered before. Take a look at Best algorithm for hashing number values?, particularly the suggestion of using MurmurHash.
I'd recommend MurmurHash because:
It's very fast.
Its distribution and avalanche characteristics are excellent for a non-cryptographic hash.
Its worst-case behavior is still pretty good.
I've used it. It doesn't suck.
edit
There was a lot of discussion about how to best port it to Delphi, on https://forums.embarcadero.com/thread.jspa?threadID=13902&tstart=0. The resulting code is available at https://forums.codegear.com/thread.jspa?threadID=14879
Delphi translation
Passes all self-tests from the original C implementation.
Jenkins hash function should help you get started.
You discard important bit of information which is the position of the character in the string. That is a bad idea, since then strings "AB" and "BA" would have same the same hash value.
Instead of simple addition, keeping it primitive, one can use expression like
hash = hash*P1 + str[i]*P2 + P3;
where Pi are some prime numbers. That's how I do it if I need a hash function quickly. I often use 7, 5 and 3 as the primes, but the numbers should be obviously adjusted (as well as initial value ofhash
) so that the result of hash function is usable to your task.For more information read the corresponding (and rather informative) Wikipedia article.
The FNV-1a hash is quick and easy to implement.
See http://www.strchr.com/hash_functions for a very good panel of hashing functions.
In Delphi implementation, here are several versions:
The first coming to mind is the one used in
TStringHash.HashOf
method from officialIniFiles.pas
unit. Including a faster asm version:The classic Kernighan & Ritchie hash from "The C programming Language", 3rd edition - not the best, but simple and efficient code.
The fast "Adler" CRC as implemented in zlib - optimized asm version here:
My own faster variant - not re-entrant, but faster since it will read by DWORDs - and an even faster asm version here:
The classic CRC32 version - you can find a very optimized asm version (using 8 tables) here: