Benchmark program for hash tables and comparison of 15 popular hash functions.

Created by Peter Kankowski
Last changed
Contributors: Nils, Ace, Won, Andrew M., and Georgi 'Sanmayce'
Filed under Algorithms

Share on social sitesReddit Digg Delicious Buzz Facebook Twitter

Hash functions: An empirical comparison

Hash tables are popular data structures for storing key-value pairs. A hash function is used to map the key value (usually a string) to array index. The functions are different from cryptographic hash functions, because they should be much faster and don't need to be resistant to preimage attack. Hashing in large databases is also left out from this article; the benchmark includes medium-size hash tables such as:

  • symbol table in a parser,
  • IP address table for filtering network traffic,
  • the dictionary in a word counting program or a spellchecker.

There are two classes of the functions used in hash tables:

  • multiplicative hash functions, which are simple and fast, but have a high number of collisions;
  • more complex functions, which have better quality, but take more time to calculate.

Hash table benchmarks usually include theoretical metrics such as the number of collisions or distribution uniformity (see, for example, hash function comparison in the Red Dragon book). Obviously, you will have a better distribution with more complex functions, so they are winners in these benchmarks.

The question is whether using complex functions gives you a faster program. The complex functions require more operations per one key, so they can be slower. Is the price of collisions high enough to justify the additional operations?

Multiplicative hash functions

Any multiplicative hash function is a special case of the following algorithm:

UINT HashMultiplicative(const CHAR *key, SIZE_T len) {
   for(UINT i = 0; i < len; ++i)
      hash = M * hash + key[i];
   return hash % TABLE_SIZE;

(Sometimes XOR operation is used instead of addition, but it does not make much difference.) The hash functions differ only by values of INITIAL_VALUE and multiplier (M). For example, the popular Bernstein's function uses INITIAL_VALUE of 5381 and M of 33; Kernighan and Ritchie's function uses INITIAL_VALUE of 0 and M of 31.

A multiplicative function works by adding together the letters weighted by powers of multiplier. For example, the hash for the word TONE will be:

  INITIAL_VALUE * M^4  +  'T' * M^3  +  'O' * M^2  +  'N' * M  +  'E'

Let's enter several similar strings and watch the output of the functions:

     Bernstein Kernighan
        (M=33)     (M=31)
 too   b88af17     1c154
 top   b88af18     1c155
 tor   b88af1a     1c157
 tpp   b88af39     1c174
a000  7c9312d6    2cd22f
a001  7c9312d7    2cd230
a002  7c9312d8    2cd231
a003  7c9312d9    2cd232
a004  7c9312da    2cd233
a005  7c9312db    2cd234
a006  7c9312dc    2cd235
a007  7c9312dd    2cd236
a008  7c9312de    2cd237
a009  7c9312df    2cd238
a010  7c9312f7    2cd24e
   a     2b606        61
  aa    597727       c20
 aaa   b885c68     17841

Too and top are different in the last letter only. The letter P is the next one after O, so the values of hash function are different by 1 (1c154 and 1c155, b88af17 and b88af18). Ditto for a000..a009.

Now let's compare top with tpp. Their hashes will be:

  INITIAL_VALUE * M^3 + 'T' * M^2 + 'O' * M + 'P'
  INITIAL_VALUE * M^3 + 'T' * M^2 + 'P' * M + 'P'

The hashes will be different by M * ('P' - 'O') = M. Similarly, when the first letters are different by x, their hashes will be different by x * M^2.

When there are less than 33 possible letters, Bernstein's function will pack them into a number (similar to Radix40 packing scheme). For example, hash table of size 333 will provide perfect hashing (without any collisions) for all three-letter English words written in small letters. In practice, the words are longer and hash tables are smaller, so there will be some collisions (situations when different strings have the same hash value).

If the string is too long to fit into the 32-bit number, the first letters will still affect the value of the hash function, because the multiplication is done modulo 2^32 (in a 32-bit register), and the multiplier is chosen to have no common divisors with 2^32 (in other words, it must be odd), so the bits will not be just shifted away.

There are no exact rules for choosing the multiplier, only some heuristics:

  • the multiplier should be large enough to accommodate most of the possible letters (e.g., 3 or 5 is too small);
  • the multiplier should be fast to calculate with shifts and additions [e.g., 33 * hash can be calculated as (hash << 5) + hash];
  • the multiplier should be odd for the reason explained above;
  • prime numbers are good multipliers.

Complex hash functions

These functions do a good job of mixing together the bits of the source word. The change in one input bit changes a half of the bits in the output (see Avalanche_effect), so the result looks completely random:

     Paul Hsieh One At Time
 too   3ad11d33  3a9fad1e  
 top   78b5a877  4c5dd09a  
 tor   c09e2021  f2aa9d35  
 tpp   3058996d  d5e9e480  
a000   7552599f  ed3859d8  
a001   3cc1d896  fef7fd57  
a002   c6ff5c9b  08a610b3  
a003   dcab7b0c  1a88b478  
a004   780c7202  3621ebaa  
a005   7eb63e3a  47db8f1d  
a006   6b0a7a17  b901717b  
a007   cb5cb1ab  caec1550  
a008   5c2a15c0  e58d4a92  
a009   33339829  f75aee2d  
a010   eb1f336e  bd097a6b  
   a   115ea782  ca2e9442  
  aa   008ad357  7081738e  
 aaa   7dfdc310  ae4f22ec

To achieve this behavior, the hash functions perform a lot of shifts, XORs, and additions. But do we need a complex function? What is faster: tolerating the collisions and resolving them with chaining, or avoiding them with a more complex function?

Test conditions

The benchmark uses separate chaining algorithm for collision resolution. Memory allocation and other "heavy" functions were excluded from the benchmarked code. The RDTSC instruction was used for benchmarking. The test was performed on Pentium-M and Core i5 processors.

The benchmark inserts some keys in the table, then looks them up in the same order as they were inserted. The test data include:

  • the list of common words from Wiktionary (500 items);
  • the list of Win32 functions from Colorer syntax highlight scheme (1992 items);
  • 500 names from a000 to a499 (imitates the names in auto-generated source code);
  • the list of common words with a long prefix and postfix;
  • all variable names from WordPress 2.3.2 source code in wp-includes folder (1842 names);
  • list of all words in Sonnets by W. Shakespeare (imitates a word counting program; 3228 words);
  • list of all words in La Peau de chagrin by Balzac (in French, UTF-8 encoding);
  • search engine IP addresses (binary).


Core i5 processor

iSCSI CRC65[105]329[415]36[112]84[106]83[92]280[368]408[584]1964[2388]322[838]1.01[1.78]
Novak unrolled76[113]404[399]43[90]127[118]125[113]322[342]459[581]2284[2430]379[969]1.26[1.68]
Paul Hsieh80[114]410[420]54[118]123[101]121[100]336[341]496[600]2351[2380]433[847]1.33[1.83]
x17 unrolled78[109]446[415]43[24]156[113]153[102]344[368]472[589]2361[2392]373[829]1.37[1.19]
One At Time85[105]562[421]58[110]221[97]220[103]392[364]511[545]2659[2346]459[795]1.72[1.75]
Arash Partow83[101]560[435]71[420]215[98]212[85]392[355]507[570]2638[2372]407[779]1.72[3.88]

Pentium-M processor

Novak unrolled90[113]517[399]56[90]169[118]164[113]398[342]575[581]2716[2430]482[969]1.18[1.68]
x17 unrolled93[109]593[415]52[24]214[113]208[102]434[368]593[589]2867[2392]486[829]1.30[1.19]
Paul Hsieh106[114]576[420]82[118]183[101]178[100]456[341]678[600]3154[2380]670[847]1.41[1.83]
Arash Partow106[101]739[435]93[420]280[98]275[85]514[355]671[570]3332[2372]543[779]1.65[3.88]
One At Time118[105]830[421]81[110]321[97]319[103]578[364]741[545]3809[2346]657[795]1.82[1.75]

Each cell includes the execution time, then the number of collisions in square brackets. Execution time is expressed in thousands of clock cycles (a lower number is better). Avg column contains the average normalized execution time (and the number of collisions).

The function by Kernighan and Ritchie is from their famous book "The C programming Language", 3rd edition; Weinberger's hash and the hash with multiplier 65599 are from the Red Dragon book. The latter function is used in gawk, sdbm, and other Linux programs. x17 is the function by Peter Kankowski (multiplier = 17; 32 is subtracted from each letter code).

As you can see from the table, the function with the lowest number of collisions is not always the fastest one.

Results on a large data set (list of all words in English Wikipedia, 12.5 million words, from the benchmark by Georgi 'Sanmayce'):

Core i5 processor

iSCSI CRC5725944[2077725]1.00[1.00]
Paul Hsieh7387317[2180206]1.29[1.05]
x17 unrolled7410443[2410605]1.29[1.16]
One At Time8338799[2087861]1.46[1.01]
Arash Partow8503299[2084572]1.49[1.00]
Novak unrolled21289919[6318611]3.72[3.05]

Pentium-M processor

x17 unrolled11321744[2410605]1.00[1.16]
Arash Partow12235396[2084572]1.08[1.00]
Paul Hsieh12992315[2180206]1.15[1.05]
One At Time13662010[2087861]1.21[1.01]
Novak unrolled37769882[6318611]3.34[3.05]

Some functions were excluded from the benchmark because of very bad performance:

  • Adler-32 (slow filling, not suitable as a hash function);
  • TwoChars (bad for machine-generated names and variable names that are similar to each other, disastrous for large data sets such as Wikipedia).

The number of collisions depending on the hash table size (for the same data set, thanks to Ace for the idea):

For 28 bits: Novak unrolled - 5.9 million collisions, Fletcher - 4.9 million collisions, Weinberger - 1.1 million collisions, x17 unrolled - 0.8 million collisions, Paul Hsieh - about 0.4 million collisions, other functions - about 0.3 million collisions

Red Dragon Book proposes the following formula for evaluating hash function quality:

sum from j=0 to m-1: b_j(b_j+1)/2 / [(n/2m)(n+2m-1)]

where bj is the number of items in j-th slot, m is the number of slots, and n is the total number of items. The sum of bj(bj + 1) / 2 estimates the number of slots your program should visit to find the required value. The denominator (n / 2m)(n + 2m − 1) is the number of visited slots for an ideal function that puts each item into a random slot. So, if the function is ideal, the formula should give 1. In reality, a good function is somewhere between 0.95 and 1.05. If it's more, there is a high number of collisions (slow!). If it's less, the function gives less collisions than the randomly distributing function, which is not bad.

Here are the results for some of our functions:

Hash function quality (using the formula from Red Dragon book). In Numbers test: K&R and Bernstein - 1.6, x65599 - 1.2, x17 and Paul Larson - 0.8, CRC-32 - 0.9. Meiyan, FNV-1a, SBox, Murmur2, Paul Hsieh, XXHfast32, and lookup3 - between 0.95 and 1.05. In other tests all functions have the quality between 0.95 and 1.05.


Complex functions by Paul Hsieh and Bob Jenkins are tuned for long keys, such as the ones in postfix and prefix tests. Note that they do not provide the best number of collisions for these tests, but do have the best time, which means that the functions are faster than the others because of loop unrolling. At the same time, they are suboptimal for short keys (words and sonnets tests).

For a word counting program, a compiler, or another application that typically handles short keys, it's often advantageous to use a simple multiplicative function such as x17 or Larson's hash. However, these functions perform badly on long keys.

Novak showed bad results on the large data set. Jesteress has a high number of collisions in numbers test.

Murmur2, Meiyan, SBox, and CRC32 provide good performance for all kinds of keys. They can be recommended as general-purpose hashing functions on x86.

Hardware-accelerated CRC (labeled iSCSI CRC in the table) is the fastest hash function on the recent Core i5/i7 processors. However, the CRC32 instruction is not supported by AMD and earlier Intel processors.

Download the source code (152 KB, MSVC++)


XORing high and low part

For table size less than 2^16, we can improve the quality of hash function by XORing high and low words, so that more letters will be taken into account:

   return hash ^ (hash >> 16);

Subtracting a constant

x17 hash function subtracts a space from each letter to cut off the control characters in the range 0x00..0x1F. If the hash keys are long and contain only Latin letters and numbers, the letters will be less frequently shifted out, and the overall number of collisions will be lower. You can even subtract 'A' when you know that the keys will be only English words.

Using larger multipliers for a compiler

Paul Hsieh noted that large multipliers may provide better results for the hash table in a compiler, because a typical source code contains a lot of one-letter variable names (i, j, s, etc.), and they will collide if the multiplier is less than the number of letters in the alphabet.

The test confirms this assumption: the function by Kernighan & Ritchie (M = 33) has lower number of collisions than x17 (M = 17), but the latter is still faster (see Variables column in the table above).

Setting hash table size to a prime number

A test showed that the number of collisions will usually be lower if you use a prime, but the calculations modulo prime take much more time than the calculations for a power of 2, so this method is impractical. Even replacing division with multiplication by reciprocal values do not help here:

Bernstein % 2K145[261]880[889]426[8030]326[214]316[226]649[697]874[1131]
Bernstein % prime186[221]1049[995]445[5621]364[194]357[217]805[800]1123[1051]
Bernstein optimized mod160[221]960[995]416[5621]341[194]334[217]722[800]969[1051]
x17 % 2K137[193]847[1002]81[340]314[244]300[228]641[863]832[1012]
x17 % prime173[256]1010[1026]104[324]356[246]339[216]760[760]1046[1064]
x17 optimized mod155[256]915[1026]96[324]330[246]315[216]691[760]930[1064]

Implementing open addressing vs. separate chaining

With open addressing, most hash functions show awkward clustering behavior in "Numbers" test:

Bernst.K&Rx17 unrollx65599FNVUnivWeinb.HsiehOne-atLookup3PartowCRC

You can avoid the worst case by using chaining for collision resolution. However, chaining requires more memory for the next item pointers, so the performance improvement does not come for free. A custom memory allocator should be usually written, because calling malloc() for a large number of small structures is suboptimal.

Some implementations (e.g., hash table in Python interpreter) store a full 32-bit hash with the item to speed up the string comparison, but this is less effective than chaining.

Peter Kankowski
Peter Kankowski

About the author

Peter is the developer of Aba Search and Replace, a tool for replacing text in multiple files. He likes to program in C with a bit of C++, also in x86 assembly language, Python, and PHP.

Created by Peter Kankowski
Last changed
Contributors: Nils, Ace, Won, Andrew M., and Georgi 'Sanmayce'


Ten recent comments are shown below. Show all comments

Georgi 'Sanmayce',

Hi Peter,

glad to share the latest-n-fastest FNV1A variant.

For a long time I knew how much more is out there, many coders shared very nice etudes, but my 'Yorikke' has something special, the ... Zennish approach embedded :P

Currently I am writing an insane matchfinder using B-trees while hashing millions of keys of order 4,6,8,10,12,14,16,18,36,64, thus a hasher of superhigh speed (FOR SMALL KEYS) is needed since the B-trees are constructed in multi-passes and billions of hash invocations of Yorikke are to be used. Latency is crucial, throughput is meh.

#define _rotl_KAZE(x, n) (((x) << (n)) | ((x) >> (32-(n))))
#define _PADr_KAZE(x, n) ( ((x) << (n))>>(n) )
#define ROLInBits 27 // 5 in r.1; Caramba: it should be ROR by 5 not ROL, from the very beginning the idea was to mix two bytes by shifting/masking the first 5 'noisy' bits (ASCII 0-31 symbols).

UINT Hash_Yorikke(const char *str, SIZE_T wrdlen)
    const UINT PRIME = 591798841;
    UINT hash32 = 2166136261;
    const char *p = str;
    long long PADDEDby8;

    for(; wrdlen >= 2*sizeof(DWORD); wrdlen -= 2*sizeof(DWORD), p += 2*sizeof(DWORD)) {
        //hash32 = (hash32 ^ (_rotl(*(DWORD *)p,ROLInBits) ^ *(DWORD *)(p+4))) * PRIME;        
    hash32 = ( _rotl_KAZE(hash32,ROLInBits) ^ *(DWORD *)(p+0) ) * PRIME;        
    hash32 = ( _rotl_KAZE(hash32,ROLInBits) ^ *(DWORD *)(p+4) ) * PRIME;        

    PADDEDby8 = _PADr_KAZE(*(long long *)(p+0), (8/1-(wrdlen&(8/1-1)))<<3);
    hash32 = ( _rotl_KAZE(hash32,ROLInBits) ^ *(DWORD *)((char *)&PADDEDby8+0) ) * PRIME;        
    hash32 = ( _rotl_KAZE(hash32,ROLInBits) ^ *(DWORD *)((char *)&PADDEDby8+(8/1)/2) ) * PRIME;        
    return hash32 ^ (hash32 >> 16);

// The very instrumental and informative page of Peter Kankowski, first column is time (smaller-better), last one is collisions (smaller-better):

500 lines read

1024 elements in the table (10 bits)

           Jesteress:         55 [  110]
              Meiyan:         56 [  102]
             Yorikke:         54 [   98] ! Best Speed, Best Dispersion ! on Core 2, 32bit executable
              FNV-1a:         69 [  124]
              Larson:         68 [   99]
              CRC-32:         65 [  101]
             Murmur2:         71 [  103]
             Murmur3:         68 [  101]
           XXHfast32:         80 [  110]
         XXHstrong32:         80 [  109]

13408 lines read

32768 elements in the table (15 bits)

           Jesteress:       1757 [ 2427]
              Meiyan:       1775 [ 2377]
             Yorikke:       1672 [ 2413] ! Best Speed, - ! on Core 2, 32bit executable
              FNV-1a:       2097 [ 2446]
              Larson:       2033 [ 2447]
              CRC-32:       2140 [ 2400]
             Murmur2:       2266 [ 2399]
             Murmur3:       2116 [ 2376]
           XXHfast32:       2428 [ 2494]
         XXHstrong32:       2431 [ 2496]

3925 lines read

8192 elements in the table (13 bits)

           Jesteress:        436 [  819]
              Meiyan:        451 [  807]
             Yorikke:        486 [  789] ! - , Best Dispersion ! on Core 2, 32bit executable
              FNV-1a:        614 [  796]
              Larson:        587 [  789]
              CRC-32:        589 [  802]
             Murmur2:        566 [  825]
             Murmur3:        549 [  818]
           XXHfast32:        704 [  829]
         XXHstrong32:        704 [  829]

500 lines read

1024 elements in the table (10 bits)

           Jesteress:         40 [  300]
              Meiyan:         32 [  125]
             Yorikke:         37 [   82] ! - , - ! on Core 2, 32bit executable
              FNV-1a:         35 [  108]
              Larson:         26 [   16]
              CRC-32:         34 [   64]
             Murmur2:         45 [  104]
             Murmur3:         42 [  104]
           XXHfast32:         53 [  102]
         XXHstrong32:         53 [  102]

500 lines read

1024 elements in the table (10 bits)

           Jesteress:         70 [  106]
              Meiyan:         74 [  112]
             Yorikke:         76 [   99] ! - , - ! on Core 2, 32bit executable
              FNV-1a:        159 [  105]
              Larson:        160 [  105]
              CRC-32:        129 [   94]
             Murmur2:         99 [  111]
             Murmur3:         98 [  105]
           XXHfast32:         76 [  106]
         XXHstrong32:         82 [  112]

500 lines read

1024 elements in the table (10 bits)

           Jesteress:         73 [  102]
              Meiyan:         77 [  106]
             Yorikke:         79 [   94] ! - , Best Dispersion ! on Core 2, 32bit executable
              FNV-1a:        165 [   94]
              Larson:        161 [   99]
              CRC-32:        135 [  107]
             Murmur2:        103 [  106]
             Murmur3:        101 [  103]
           XXHfast32:         77 [  103]
         XXHstrong32:         82 [  102]

3228 lines read

8192 elements in the table (13 bits)

           Jesteress:        357 [  585]
              Meiyan:        366 [  588]
             Yorikke:        349 [  536] ! Best Speed, - ! on Core 2, 32bit executable
              FNV-1a:        419 [  555]
              Larson:        404 [  583]
              CRC-32:        433 [  563]
             Murmur2:        471 [  566]
             Murmur3:        443 [  555]
           XXHfast32:        493 [  491]
         XXHstrong32:        493 [  491]

1842 lines read

4096 elements in the table (12 bits)

           Jesteress:        249 [  366]
              Meiyan:        256 [  350]
             Yorikke:        240 [  351] ! Best Speed, - ! on Core 2, 32bit executable
              FNV-1a:        318 [  374]
              Larson:        313 [  366]
              CRC-32:        309 [  338]
             Murmur2:        318 [  383]
             Murmur3:        299 [  334]
           XXHfast32:        336 [  347]
         XXHstrong32:        339 [  355]

Georgi 'Sanmayce',
Hashing Faster than SSE4.2 iSCSI-CRC

The feed:



Dummy me, had to fix v2, now everything is OK, my excuse - yesterday, have been distracted the whole day.

So, here comes v3:

#define _rotl_KAZE(x, n) (((x) << (n)) | ((x) >> (32-(n))))
#define _PADr_KAZE(x, n) ( ((x) << (n))>>(n) )
#define ROLInBits 27 // 5 in r.1; Caramba: it should be ROR by 5 not ROL, from the very beginning the idea was to mix two bytes by shifting/masking the first 5 'noisy' bits (ASCII 0-31 symbols).
// CAUTION: Add 8 more bytes to the buffer being hashed, usually malloc(...+8) - to prevent out of boundary reads!
uint32_t FNV1A_Hash_Yorikke_v3(const char *str, uint32_t wrdlen)
    const uint32_t PRIME = 591798841;
    uint32_t hash32 = 2166136261;
    uint64_t PADDEDby8;
    const char *p = str;
    for(; wrdlen > 2*sizeof(uint32_t); wrdlen -= 2*sizeof(uint32_t), p += 2*sizeof(uint32_t)) {
        hash32 = ( _rotl_KAZE(hash32,ROLInBits) ^ (*(uint32_t *)(p+0)) ) * PRIME;        
        hash32 = ( _rotl_KAZE(hash32,ROLInBits) ^ (*(uint32_t *)(p+4)) ) * PRIME;        
		// Here 'wrdlen' is 1..8
		PADDEDby8 = _PADr_KAZE(*(uint64_t *)(p+0), (8-wrdlen)<<3); // when (8-8) the QWORD remains intact
	        hash32 = ( _rotl_KAZE(hash32,ROLInBits) ^ *(uint32_t *)((char *)&PADDEDby8+0) ) * PRIME;        
	        hash32 = ( _rotl_KAZE(hash32,ROLInBits) ^ *(uint32_t *)((char *)&PADDEDby8+4) ) * PRIME;        
    return hash32 ^ (hash32 >> 16);
// Last touch: 2019-Oct-03, Kaze

Georgi 'Sanmayce',

Peter, I see no ways to better the 32bit code hashers, so the fastest known to me 32bit hasher in 64bit code is:


Mohit Soni,

What is the size of Bucket used in all the hash function mentioned in the graph named (hash function quality using red dragon book) in this blog?. It is requested to answer the query please.

Peter Kankowski,

Hello Mohit, thank you, it's a good question. The number of buckets (slots) were 2 * the number of items rounded to the next multiple of two. For example, in the "numbers" test there are 500 items in the table, so the number of buckets (hash table size) is 2 * 512 = 1024. Hope this helps

Mohit Soni,

Thanks for your response

Mohit Soni,

I just wanted to know the exact value for the uniform distribution of different hash function in the graph named (hash function quality using red dragon book), Can you please provide the exact values for which you plotted the graph.

It would be very helpful of you.

Mohit Soni,

Can you please provide me with the exact value in the hash function quality graph just for numbers dataset? It would be really helpful of you.

Thanks in advance @Peter Kankowski.


Where’s the Wikipedia wordlist (or where does one get it from), and, more importantly, the OA test code?

I’m looking for a good OA spread/avalanche combo that’s cheap enough but doesn’t invoke UB or IB in C and is extremely portable (so it has to read by bytes, which makes a CRC surprisingly expensive, 2.39 to one-at-a-time’s 2.21 on my test borrowed windows system (don’t normally have one but your source is for it…))

Arash Partow,

Here are some interesting hash functions that can be added to your comparison suite:


Your name: