原文
7 L& }( g9 t' xhttp://blog.csai.cn/user3/50125/archives/2009/35638.html , @' n& `7 T- r/ t" X& |
7 |4 N' }* t; ]+ Y& A
[pre]文章转摘自http://www.cmykrgb123.cn/blog/string-hash-compare/ " r/ C. s5 W4 x; `5 B
2 c) l. J, E: T9 [常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法。这些函数使用 - G. Z$ m% y" O0 n) ]
位运算使得每一个字符都对最后的函数值产生影响。另外还有以MD5和SHA1为代表的杂凑函数,
/ i* f# J3 G$ s( f; ?这些函数几乎不可能找到碰撞。 % K% p% n# b; _+ c0 U
常用字符串哈希函数有BKDRHash,APHash,DJBHash,JSHash,RSHash,SDBMHash,
8 M9 K% \2 X8 K3 H( I1 }' h _ E* yPJWHash,ELFHash等等。对于以上几种哈希函数,我对其进行了一个小小的评测。 # Q/ s7 Z3 g+ ?
Hash函数 | 数据1 | 数据2 | 数据3 | 数据4 | 数据1得分 | 数据2得分 | 数据3得分 | 数据4得分 | 平均分 | 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 | 506 | 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 |
& p7 ~; m' ^: ?$ `% `其中数据1为100000个字母和数字组成的随机串哈希冲突个数。数据2为100000个有意义的英文句 g3 m) {+ j8 O. K9 W4 K" _
子哈希冲突个数。数据3为数据1的哈希值与1000003(大素数)求模后存储到线性表中冲突的个数。
+ r, L8 q7 X" Y: n2 O N数据4为数据1的哈希值与10000019(更大素数)求模后存储到线性表中冲突的个数。 1 {3 t2 ]; Q5 M3 A. T
经过比较,得出以上平均得分。平均数为平方平均数。可以发现,BKDRHash无论是在实际效果还是
1 P4 p1 m# x' n编码实现中,效果都是最突出的。APHash也是较为优秀的算法。DJBHash,JSHash,RSHash与
1 [( w/ O" w% oSDBMHash各有千秋。PJWHash与ELFHash效果最差,但得分相似,其算法本质是相似的。
+ I- m$ i# {2 W4 C( g在信息修竞赛中,要本着易于编码调试的原则,个人认为BKDRHash是最适合记忆和使用的。
. \: w* k- T7 j9 P+ S* b& vCmYkRgB123原创,欢迎建议、交流、批评和指正。 9 V. @1 _. w0 `" B1 }( S
' q5 m l& } I0 l+ U! P& R
附:各种哈希函数的C语言程序代码 5 T b, ~: F' u) }6 d! M7 D
7 e% f8 C6 p9 E2 J! `unsigned int SDBMHash(char *str)
, Z) a W, @1 i0 c5 g{ ' I7 ^. U) t/ k- F9 c
unsigned int hash = 0;
& r$ m: k, ]1 p/ y5 B ( A# D$ ~, f; F6 D5 @, L
while (*str) 1 P, h; I- Q, x
{ 6 v. ^3 D! T3 [9 [% M+ j9 X
// equivalent to: hash = 65599*hash + (*str++); " u V3 Q* ?3 A, g
hash = (*str++) + (hash << 6) + (hash << 16) - hash;
3 b& m4 O: j" | }
5 N! H) [- a! B/ Q8 I) @
8 |$ g! C+ ]" e5 o+ J- C3 u- D- T return (hash & 0x7FFFFFFF); # @- l2 N7 [3 G, o* t6 }! e/ q
} : |, P& T; Q# q5 m1 J9 o7 r( u) }7 m
" f, q2 Y! n' g8 W
// RS Hash ' |; Z$ t$ o+ n; j+ E/ U0 C; K7 q
unsigned int RSHash(char *str) 0 u. j; p- [ h* Y' V7 v$ ? f
{
+ R5 P8 l) c) g+ {" @1 ~+ p+ } f unsigned int b = 378551;
( B8 d% C! ]$ E5 R unsigned int a = 63689; ) P/ `8 V3 r" N
unsigned int hash = 0;
7 L7 r# T) g, I7 ^ , ^( ?0 W, P; l: K
while (*str)
) n5 ]( O3 X" w7 S% c/ G5 m1 a { & f- S- j* T- K6 n2 Y
hash = hash * a + (*str++);
+ G c# s1 b8 W8 }) j0 s, l a *= b;
! N R/ b* s3 X+ n3 C2 R3 @% _ }
% z2 ^! F* ]; B9 P1 B; f
% H. y7 g5 z' w+ N/ @- f @, N! A return (hash & 0x7FFFFFFF);
9 P2 ^& z: e: A. f6 y} # d( A6 k- v, A4 v
c& M8 h3 q" { b
// JS Hash ! u* h8 {; A: G9 k8 k* d% X
unsigned int JSHash(char *str)
4 q7 S5 s) }2 s% v0 `{ 9 J. E8 I' r) B& j+ R8 k3 K
unsigned int hash = 1315423911; ' m8 Z% a9 ^) r3 J' M; Y3 i( C
9 H$ J9 A4 V3 E. a, Z6 e v while (*str) 7 R. `& e! q3 G' q* C
{ / ^) \1 Q, }$ Y6 ?
hash ^= ((hash << 5) + (*str++) + (hash >> 2));
1 s# v8 e/ V* v1 N5 G }
+ E3 N5 c" o% l; \ . m' [' f+ b' v
return (hash & 0x7FFFFFFF); ! y# |* e* y5 r: E
}
: t+ f* S# K. y
$ e! c8 m! G$ ]/ S$ T- O// P. J. Weinberger Hash
( q4 Y! ]: U9 E/ j# Munsigned int PJWHash(char *str)
2 j( x' x0 m2 }6 n- e; j7 e# c{
# {8 O% s! L I, N# k I4 h- v unsigned int BitsInUnignedInt = (unsigned int)(sizeof(unsigned int) * 8);
7 F7 R( r' x E3 @6 w/ F) A- B- S unsigned int ThreeQuarters = (unsigned int)((BitsInUnignedInt * 3) / 4);! K6 e! {6 a- h; G+ S) D7 R$ R
unsigned int OneEighth = (unsigned int)(BitsInUnignedInt / 8);' X; }" t' s0 \0 n# t" Z
unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnignedInt [pre] - 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 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 unsigned int BKDRHash(char *str){ unsigned int seed = 131; // 31 131 1313 13131 131313 etc.. unsigned int hash = 0; while (*str) { hash = hash * seed + (*str++); } return (hash & 0x7FFFFFFF);} // DJB Hash unsigned int DJBHash(char *str){ unsigned int hash = 5381; while (*str) { hash += (hash << 5) + (*str++); } return (hash & 0x7FFFFFFF);} // AP Hash 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 << 11) ^ (*str++) ^ (hash >> 5))); } } return (hash & 0x7FFFFFFF);}[/pre] |