Commonly used hash algorithms

Source: Internet
Author: User

1, Rshashunsigned int rshash (const std::string& str) {unsigned int b = 378551;   unsigned int a = 63689;    unsigned int hash = 0;      for (std::size_t i = 0; i < str.length (); i++) {hash = hash * a + str[i];   A = a * b; } return hash;} 2, Jshashunsigned int jshash (const std::string& str) {unsigned int hash = 1315423911;   for (std::size_t i = 0; i < str.length (); i++) {hash ^= (hash << 5) + str[i] + (hash >> 2)); } return hash;} 3, Pjwhashunsigned int pjwhash (const std::string& str) {   unsigned int bitsinunsignedint = (unsigned int) ( sizeof (unsigned int) * 8);    unsigned int threequarters     = (unsigned int) (( Bitsinunsignedint  * 3)/4);    unsigned int oneeighth          = (unsigned int) (BITSINUNSIGNEDINT/8);    unsigned int highbits      & nbsp;   = (unsigned int) (0xFFFFFFFF) << (bitsinunsignedint-oneeighth);    unsigned int hash              = 0;    unsigned int Test              = 0;     for (std::size_t i = 0; i < str.length (); i++)    {       hash = (hash << oneeighth) + Str[i];      &nbsP;if (test = hash & highbits)  != 0)       {          hash = ((hash ^ (test >> threequarters)) & (~highbits));       }   }   return Hash;}   4, Elfhashunsigned int elfhash (const std::string& str) {unsigned int hash = 0;    unsigned int x = 0;      for (std::size_t i = 0; i < str.length (); i++) {hash = (hash << 4) + str[i];      if ((x = hash & 0xf0000000l)! = 0) {hash ^= (x >> 24);   } hash &= ~x; } return hash;} 5, Bkdrhashunsigned int bkdrhash (const std::string& str) {unsigned int seed = 131;//131 1313 13131 131313 etc..    unsigned int hash = 0;   for (std::size_t i = 0; i < str.length (); i++) {hash = (hash * seed) + str[i]; } return hash;} 6, Sdbmhashunsigned int sdbmhash (const std::string& str) {unsigned int hash = 0;   for (std::size_t i = 0; i < str.length (); i++) {hash = Str[i] + (hash << 6) + (hash << +)-hash; } return hash;} 7, Djbhash(TIMES33)-This is used very much and many libraries use it.    unsigned int djbhash (const std::string& str) {unsigned int hash = 5381;   for (std::size_t i = 0; i < str.length (); i++) {hash = ((hash << 5) + hash) + str[i]; } return hash;} 8, Dekhashunsigned int dekhash (const std::string& str) {unsigned int hash = static_cast (Str.length ());   for (std::size_t i = 0; i < str.length (); i++) {hash = ((hash << 5) ^ (hash >>)) ^ str[i]; } return hash;} 9, Bphashunsigned int bphash (const std::string& str) {unsigned int hash = 0;   for (std::size_t i = 0; i < str.length (); i++) {hash = hash << 7 ^ str[i]; } return hash;} 10, Fnvhashunsigned int fnvhash (const std::string& str) {const unsigned int fnv_prime = 0X811C9DC5;   unsigned int hash = 0;      for (std::size_t i = 0; i < str.length (); i++) {hash *= fnv_prime;   Hash ^= str[i]; } return hash;} 11, Aphashunsigned int aphash (const std::string& str) {unsigned int hash = 0xAAAAAAAA; for (std::size_t i = 0; i < str.length (); i++) {Hash ^= ((i & 1) = = 0)? ((hash << 7) ^ str[i] * (hash >> 3)): ((hash << one) + (Str[i] ^ (Has   H >> 5))); } return hash;} 12, MurmurHash -A very new hashing algorithm, which should be the most efficient hashing algorithm at present, has a high usage rate. (pseudo code from Wikipedia) MURMUR2 (key, Len, Seed)     m <-0x5bd1e995    r <-24     seed  0x9747b28c    hash <-seed XOR len    for Each fourbytechunk of key        k <-fourbytechunk         k <-k * m        k <-k XOR (k >> R) & Nbsp;       k <-k * m          Hash <-hash * m        hash <-hash XOR k      With any remainingbytesinkey        remainingbytes   Swapendianorderof (Remainingbytesinkey)          hash <-Hash XOR Remainingbytes       &nbsP;hash <-Hash * M     hash <-hash XOR (hash >>)     hash &lt ;-Hash * M    hash <-hash XOR (hash >>)    murmurhash algorithm: High computational performance, low collision rate, Founded in 2008 by Austin Appleby, it has been applied to open source systems such as Hadoop, libstdc++, Nginx, and libmemcached. Appleby was hired by Google in 2011, then Google introduced its variant of the Cityhash algorithm.  

Official website: https://sites.google.com/site/murmurhash/

MurmurHash algorithm, claiming to be super fast hash algorithm, is the FNV 4-5 times. The official figures are as follows:

oneatatime–354.163715 mb/sec
fnv–443.668038 mb/sec
superfasthash–985.335173 mb/sec
lookup3–988.080652 mb/sec
MurmurHash 1.0–1363.293480 mb/sec
MurmurHash 2.0–2056.885653 mb/sec

But there are also articles claiming that the MurmurHash is faster than DJB when the length of the key is greater than 10 bytes. "From a computational speed, the MurmurHash is only available for long-length characters of known length."

Commonly used hash algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.