What are the disadvantages of hashing function using multiplication method

前端 未结 1 1428
旧时难觅i
旧时难觅i 2021-01-03 01:27

There are two basic methods for implementing a hash function that are cited in pretty much every text book and CS courses:

  1. Division method whe
相关标签:
1条回答
  • 2021-01-03 01:41

    Division method is used in conjunction with hash table algorithms which require prime table size - for example, open addressing with double hashing or QHash, when you anyway need to divide the key, or it's hash, by table size to get the index.

    Multiplication method is suitable when the table size is a power of two, then getting the index from the hash could be implemented as bitwise AND operation, therefore the whole path of computing the table index by the key, with multiplication hashing, is very fast. You can explore some actual implementations by searching for magic constant 2654435769 on Github.

    There is a recent trend of using MurmurHash3 avalanche procedure instead of multiplication method:

    int hash = key;
    hash ^= (hash >> 16);
    hash *= 0x85ebca6b;
    hash ^= (hash >> 13);
    hash *= 0xc2b2ae35;
    hash ^= (hash >> 16);
    // see this code and the version for 64 bits here:
    // https://smhasher.googlecode.com/svn/trunk/MurmurHash3.cpp
    

    Because it is just a bit slower, but considered more robust to bad key distribution. That is why you might get wrong (or right?) impression that multiplication method is used unfairly rarely.

    0 讨论(0)
提交回复
热议问题