http://blog.csdn.net/kingstar158/article/details/8028635
Because of the work needs, for tens other data, the use of stl::map there is really a problem of efficiency, and finally use Boost::unordered_map replace the former, found that there is a great improvement in efficiency, but still can not meet our needs;
Stl::map underlying algorithm: B+tree implementation
Boost::unordered_map Bottom algorithm: hash implementation
So you might want to write hash function for different data types to optimize the efficiency of the search and insertion, write yourself, really do not have this strength, only on Google to find the ancestors of the ingenious algorithm to learn from:
Commonly used string hash functions have bkdrhash,aphash,djbhash,jshash,rshash,sdbmhash,pjwhash,elfhash and so on;
Have predecessors did the evaluation: the following
Ash function |
Data 1 |
Data 2 |
Data 3 |
Data 4 |
Data 1 Score |
Data 2 Score |
Data 3 Score |
Data 4 Score |
Average score |
Bkdrhash |
2 |
0 |
4774 |
481 |
96.55 |
100 |
90.95 |
82.05 |
92.64 |
Aphash |
2 |
3 |
4754 |
493 |
96.55 |
88.46 |
100 |
51.28 |
86.28 |
Djbhash |
2 |
2 |
4975 |
474 |
96.55 |
92.31 |
0 |
100 |
83.43 |
Jshash |
1 |
4 |
4761 |
40W |
100 |
84.62 |
96.83 |
17.95 |
81.94 |
Rshash |
1 |
0 |
4861 |
505 |
100 |
100 |
51.58 |
20.51 |
75.96 |
Sdbmhash |
3 |
2 |
4849 |
504 |
93.1 |
92.31 |
57.01 |
23.08 |
72.41 |
Pjwhash |
30 |
26 |
4878 |
513 |
0 |
0 |
43.89 |
0 |
21.95 |
Elfhash |
30 |
26 |
4878 |
513 |
0 |
0 |
43.89 |
0 |
21.95 |
Where data 1 is the number of random string hash collisions consisting of 100,000 letters and numbers. Data 2 is the number of 100,000 meaningful English sentence hash collisions. Data 3 is the number of conflicts that are stored in a linear table after the hash value of data 1 is modeled with 1000003 (large prime). Data 4 is the number of conflicts that are stored in a linear table after the hash value of data 1 is modeled with 10000019 (greater prime).
C language implementations of various hash function:
[CPP]View Plaincopyprint?
- unsigned int sdbmhash (char *str)
- {
- unsigned int hash = 0;
- While (*STR)
- {
- //Equivalent To:hash = 65599*hash + (*str++);
- hash = (*str++) + (hash << 6) + (hash << +)-hash;
- }
- return (hash & 0x7FFFFFFF);
- }
- RS Hash Function
- unsigned int rshash (char *str)
- {
- unsigned int b = 378551;
- unsigned int a = 63689;
- unsigned int hash = 0;
- While (*STR)
- {
- hash = hash * A + (*str++);
- a *= b;
- }
- return (hash & 0x7FFFFFFF);
- }
- JS Hash Function
- unsigned int jshash (char *str)
- {
- unsigned int hash = 1315423911;
- While (*STR)
- {
- Hash ^= (Hash << 5) + (*str++) + (hash >> 2));
- }
- return (hash & 0x7FFFFFFF);
- }
- P. J. Weinberger Hash Function
- unsigned int pjwhash (char *str)
- {
- unsigned int bitsinunignedint = (unsigned int) (sizeof (unsigned int) * 8);
- unsigned int threequarters = (unsigned int) ((Bitsinunignedint * 3)/4);
- unsigned int oneeighth = (unsigned int) (BITSINUNIGNEDINT/8);
- unsigned int highbits = (unsigned int) (0xFFFFFFFF) << (bitsinunignedint-oneeighth);
- unsigned int hash = 0;
- unsigned int test = 0;
- While (*STR)
- {
- hash = (hash << oneeighth) + (*str++);
- if (test = hash & highbits)! = 0)
- {
- hash = ((hash ^ (test >> threequarters)) & (~highbits));
- }
- }
- return (hash & 0x7FFFFFFF);
- }
- ELF Hash Function
- unsigned int elfhash (char *str)
- {
- unsigned int hash = 0;
- unsigned int x = 0;
- While (*STR)
- {
- hash = (hash << 4) + (*str++);
- if ((x = hash & 0xf0000000l)! = 0)
- {
- Hash ^= (x >> 24);
- Hash &= ~x;
- }
- }
- return (hash & 0x7FFFFFFF);
- }
- BKDR Hash Function
- unsigned int bkdrhash (char *str)
- {
- unsigned int seed = 131; //131 1313 13131 131313 etc..
- unsigned int hash = 0;
- While (*STR)
- {
- hash = hash * seed + (*str++);
- }
- return (hash & 0x7FFFFFFF);
- }
- DJB Hash Function
- unsigned int djbhash (char *str)
- {
- unsigned int hash = 5381;
- While (*STR)
- {
- Hash + = (hash << 5) + (*str++);
- }
- return (hash & 0x7FFFFFFF);
- }
- AP Hash Function
- unsigned int aphash (char *str)
- {
- unsigned int hash = 0;
- int i;
- For (i=0; *str; i++)
- {
- if ((i & 1) = = 0)
- {
- Hash ^= (Hash << 7) ^ (*str++) ^ (hash >> 3));
- }
- Else
- {
- Hash ^= ((hash << one) ^ (*str++) ^ (hash >> 5));
- }
- }
- return (hash & 0x7FFFFFFF);
- }
- Https://www.byvoid.com/blog/string-hash-compare/
hash function comparison