各種字串Hash函數比較

來源:互聯網
上載者:User

文章轉摘自http://www.cmykrgb123.cn/blog/string-hash-compare/

 

常用的字串Hash函數還有ELFHash,APHash等等,都是十分簡單有效方法。這些函數使用

位元運算使得每一個字元都對最後的函數值產生影響。另外還有以MD5和SHA1為代表的雜湊函數,

這些函數幾乎不可能找到碰撞。

常用字串雜湊函數有BKDRHash,APHash,DJBHash,JSHash,RSHash,SDBMHash,

PJWHash,ELFHash等等。對於以上幾種雜湊函數,我對其進行了一個小小的評測。

 

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

 

 

其中資料1為100000個字母和數字組成的隨機串雜湊衝突個數。資料2為100000個有意義的英文句

子雜湊衝突個數。資料3為資料1的雜湊值與1000003(大素數)求模後儲存到線性表中衝突的個數。

資料4為資料1的雜湊值與10000019(更大素數)求模後儲存到線性表中衝突的個數。

經過比較,得出以上平均得分。平均數為平方平均數。可以發現,BKDRHash無論是在實際效果還是

編碼實現中,效果都是最突出的。APHash也是較為優秀的演算法。DJBHash,JSHash,RSHash與

SDBMHash各有千秋。PJWHash與ELFHash效果最差,但得分相似,其演算法本質是相似的。

在資訊修競賽中,要本著易於編碼調試的原則,個人認為BKDRHash是最適合記憶和使用的。

CmYkRgB123原創,歡迎建議、交流、批評和指正。

 

附:各種雜湊函數的C語言程式碼

 

unsigned int SDBMHash(char *str){    unsigned int hash = 0;    while (*str)    {        // equivalent to: hash = 65599*hash + (*str++);        hash = (*str++) + (hash << 6) + (hash << 16) - hash;    }    return (hash & 0x7FFFFFFF);}// RS Hash 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 unsigned int JSHash(char *str){    unsigned int hash = 1315423911;    while (*str)    {        hash ^= ((hash << 5) + (*str++) + (hash >> 2));    }    return (hash & 0x7FFFFFFF);}// P. J. Weinberger Hash 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 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);}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.