原文 / j2 H g( P; x2 q& h' d0 E
http://blog.csai.cn/user3/50125/archives/2009/35638.html
5 x; Q; ]% p& B; L6 x3 M" }
) z0 k0 j. A( T[pre]文章转摘自http://www.cmykrgb123.cn/blog/string-hash-compare/
' q% a# `( g' k : S" }1 k( f6 D
常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法。这些函数使用
/ c B) C2 w+ e9 D2 `6 s" z& i位运算使得每一个字符都对最后的函数值产生影响。另外还有以MD5和SHA1为代表的杂凑函数,
2 r1 Q# M% M4 h2 Z8 C) V6 m& p$ B这些函数几乎不可能找到碰撞。 4 p5 U2 k1 J# ]6 t) Z$ g) c6 U- c% B
常用字符串哈希函数有BKDRHash,APHash,DJBHash,JSHash,RSHash,SDBMHash, 4 }! Y/ B1 M. u4 Z
PJWHash,ELFHash等等。对于以上几种哈希函数,我对其进行了一个小小的评测。 ' X: M* L2 w A8 P# [
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 |
5 T/ _: W" T: W- v0 \8 \* T7 l9 P其中数据1为100000个字母和数字组成的随机串哈希冲突个数。数据2为100000个有意义的英文句
+ T+ P* F, X6 n. r0 ~: [; [- Z/ _4 x子哈希冲突个数。数据3为数据1的哈希值与1000003(大素数)求模后存储到线性表中冲突的个数。
. c" s- \9 V5 I! D9 |" r数据4为数据1的哈希值与10000019(更大素数)求模后存储到线性表中冲突的个数。 : M+ R( X! q0 l3 d* n8 ^
经过比较,得出以上平均得分。平均数为平方平均数。可以发现,BKDRHash无论是在实际效果还是 ' R# k9 W- n" L. A( R- o9 C7 v
编码实现中,效果都是最突出的。APHash也是较为优秀的算法。DJBHash,JSHash,RSHash与
- E" o- N% F( U. C5 zSDBMHash各有千秋。PJWHash与ELFHash效果最差,但得分相似,其算法本质是相似的。
. M, q5 X9 p: I" j; ^- |8 Z& H在信息修竞赛中,要本着易于编码调试的原则,个人认为BKDRHash是最适合记忆和使用的。
. q( e9 i2 P6 }' v* ICmYkRgB123原创,欢迎建议、交流、批评和指正。 ' p3 z% p. y0 ?* G
1 Z- R* g0 }# P9 r' @& K( f" k附:各种哈希函数的C语言程序代码 / q8 F% @2 s& @, r. g1 Q4 J2 r' _
* {3 |+ l# v8 Z- aunsigned int SDBMHash(char *str) 1 L- [; |7 ^, ^
{ : X9 [4 g# j% P |! L
unsigned int hash = 0; ' C, ~8 E; O& o* s6 Z
+ {0 e; d s4 Y" W8 P while (*str)
' F5 p4 F& U1 i: \. a- G { + a5 V: v& u! w9 ~5 _* C6 G
// equivalent to: hash = 65599*hash + (*str++); 1 p2 v/ g3 k# z* h; [& S2 _6 c5 \; p
hash = (*str++) + (hash << 6) + (hash << 16) - hash;
+ i0 ?) Z4 p( R# S' \9 n }
z; o0 ~4 Q5 m9 \ ) j6 ]/ Y: R: n: u: z4 O& x3 b
return (hash & 0x7FFFFFFF);
: Q9 O. q3 X/ _} - ^& b2 z9 w. R3 }
$ j5 P4 R X3 ]8 K7 D7 ~+ D( {
// RS Hash
3 R+ b8 R" ]! n! ounsigned int RSHash(char *str) t2 O. w J D/ H i
{
6 T( i( N5 B3 x" T1 W4 Y* ^ unsigned int b = 378551; + ?3 T I) A: K; I P( l. r! ^ ?
unsigned int a = 63689; ?' W! \* b6 Q8 l6 ^
unsigned int hash = 0;
7 ^# C R) j6 ?: n: t8 b& M0 V1 I4 l
A1 p( D# I5 C$ \' a1 q while (*str) F/ {; t% H+ x
{
& J' c0 B6 c' {1 w. W6 ` hash = hash * a + (*str++); 0 m- i3 z- t7 a9 d& |
a *= b;
g; R4 l( V: j: t: H } 4 N: O" a' B. T+ v
f. p# p r* m" m5 S) f$ S return (hash & 0x7FFFFFFF);
0 e, m2 E( Z8 v# O; D, e4 r5 S} / C9 O( B: k3 j# q) }" ~1 Q
6 c0 O; q3 ]+ Y* q% M6 v$ U" x// JS Hash
; N3 K: c' \/ t' tunsigned int JSHash(char *str) : ^* C/ X5 h# G N. `
{
9 ~; {% m8 {2 G; r1 H* \) O unsigned int hash = 1315423911;
) }/ M" b- f2 h G - J6 M2 @4 |) f) f% w
while (*str) ! z5 P1 k0 `1 g7 ?% E
{
7 v4 R( P+ L( n$ ] hash ^= ((hash << 5) + (*str++) + (hash >> 2));
$ R# X. n. G& U8 B' u } / b; B8 r& q3 [
$ t- {1 U7 X1 ]( o0 E8 z
return (hash & 0x7FFFFFFF);
+ H, z, ]% }' F! l: i} ( ]; X) J. S$ Z
! @7 r* P" I7 j3 k// P. J. Weinberger Hash 3 U4 { |) ^4 S0 p+ y
unsigned int PJWHash(char *str)% ]9 r8 _2 I9 `# T7 R& e5 z; m. U
{* w( V0 @2 ~$ G1 `" {# [2 A2 a
unsigned int BitsInUnignedInt = (unsigned int)(sizeof(unsigned int) * 8);6 L( u J! Y3 h' @+ z9 Q
unsigned int ThreeQuarters = (unsigned int)((BitsInUnignedInt * 3) / 4);
4 T8 l$ Z: G4 U1 A8 @ unsigned int OneEighth = (unsigned int)(BitsInUnignedInt / 8);
" F ?1 g! y9 { ]. Y 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] |