12

I'm looking for a high-speed hashing function with good (i.e near uniform) distribution for use in a hash table implementation.

The hash table will be used exclusively for storing values with an integer key.

Can i just use the lower few bits of the integer as the hash?

e.g int key = n & 15; and create an array with 16 slots to store them.

Any recommendations?

3
  • 20
    There is no such thing as a perfect hash function. However, if you want some algorithms with corresponding source code, see here: partow.net/programming/hashfunctions/index.html Commented May 29, 2012 at 10:39
  • Taking the lowest bits is probably the worst thing to do. (but: it all depends on the range of values you expect in your int key) Try to mix in the upper bits as well, or multiply with a large enough (odd, prime) number. Know what to expect and measure it. Commented May 29, 2012 at 11:19
  • Post your comment as an answer and I will accept it. Commented May 31, 2012 at 9:35

2 Answers 2

3

You can see here xxhash

Your mentioned hash function is very fast, but it is very bad too. If you want a "stupid" hash function maybe you can consider the modulus.

Example:

int key = item % size_of_hash_table
Sign up to request clarification or add additional context in comments.

Comments

0

Well, last night I made a versatile hash test (in C) which covers several top-gun hashers and 38 different keys.

You all are welcome to benchmark it at: http://www.overclock.net/t/1319572/benchmarking-the-fastest-hash-function/0_20#post_18495990

I would be glad for revealing how Intel vs AMD and Intel 12.1 compiler vs Microsoft 16 (VS2010) compiler combinations behave with your help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.