天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

php-perl哈希算法實現(times33哈希算法)

復制代碼 代碼如下:
APR_DECLARE_NONSTD(unsigned int) apr_hashfunc_default(const char *char_key,
                                                      apr_ssize_t *klen)
{
    unsigned int hash = 0;
    const unsigned char *key = (const unsigned char *)char_key;
    const unsigned char *p;
    apr_ssize_t i;

    /*
     * This is the popular `times 33' hash algorithm which is used by
     * perl and also appears in Berkeley DB. This is one of the best
     * known hash functions for strings because it is both computed
     * very fast and distributes very well.
     *
     * The originator may be Dan Bernstein but the code in Berkeley DB
     * cites Chris Torek as the source. The best citation I have found
     * is "Chris Torek, Hash function for text in C, UseNET message
     * <[email protected]> in comp.lang.c , October, 1990." in Rich
     * Salz's USENIX 1992 paper about INN which can be found at
     * .
     *
     * The magic of number 33, i.e. why it works better than many other
     * constants, prime or not, has never been adequately explained by
     * anyone. So I try an explanation: if one experimentally tests all
     * multipliers between 1 and 256 (as I did while writing a low-level
     * data structure library some time ago) one detects that even
     * numbers are not useable at all. The remaining 128 odd numbers
     * (except for the number 1) work more or less all equally well.
     * They all distribute in an acceptable way and this way fill a hash
     * table with an average percent of approx. 86%.
     *
     * If one compares the chi^2 values of the variants (see
     * Bob Jenkins ``Hashing Frequently Asked Questions'' at
     * http://burtleburtle.NET/bob/hash/hashfaq.html for a description
     * of chi^2), the number 33 not even has the best value. But the
     * number 33 and a few other equally good numbers like 17, 31, 63,
     * 127 and 129 have nevertheless a great advantage to the remaining
     * numbers in the large set of possible multipliers: their multiply
     * operation can be replaced by a faster operation based on just one
     * shift plus either a single addition or subtraction operation. And
     * because a hash function has to both distribute good _and_ has to
     * be very fast to compute, those few numbers should be preferred.
     *
     *                  -- Ralf S. Engelschall
     */

    if (*klen == APR_HASH_KEY_STRING) {
        for (p = key; *p; p++) {
            hash = hash * 33 + *p;
        }
        *klen = p - key;
    }
    else {
        for (p = key, i = *klen; i; i--, p++) {
            hash = hash * 33 + *p;
        }
    }
    return hash;
}

對函數注釋部分的翻譯: 這是很出名的times33哈希算法,此算法被perl語言采用并在Berkeley DB中出現.它是已知的最好的哈希算法之一,在處理以字符串為鍵值的哈希時,有著極快的計算效率和很好哈希分布.最早提出這個算法的是Dan Bernstein,但是源代碼確實由Clris Torek在Berkeley DB出實作的.我找到的最確切的引文中這樣說”Chris Torek,C語言文本哈希函數,UseNET消息<<[email protected]> in comp.lang.c ,1990年十月.”在Rich Salz于1992年在USENIX報上發表的討論INN的文章中提到.這篇文章可以在上找到. 33這個奇妙的數字,為什么它能夠比其他數值效果更好呢?無論重要與否,卻從來沒有人能夠充分說明其中的原因.因此在這里,我來試著解釋一下.如果某人試著測試1到256之間的每個數字(就像我前段時間寫的一個底層數據結構庫那樣),他會發現,沒有哪一個數字的表現是特別突出的.其中的128個奇數(1除外)的表現都差不多,都能夠達到一個能接受的哈希分布,平均分布率大概是86%. 如果比較這128個奇數中的方差值(gibbon:統計術語,表示隨機變量與它的數學期望之間的平均偏離程度)的話(見Bob Jenkins的<哈希常見疑問>http://burtleburtle.NET/bob/hash/hashfaq.html,中對平方差的描述),數字33并不是表現最好的一個.(gibbon:這里按照我的理解,照常理,應該是方差越小穩定,但是由于這里不清楚作者方差的計算公式,以及在哈希離散表,是不是離散度越大越好,所以不得而知這里的表現好是指方差值大還是指方差值小),但是數字33以及其他一些同樣好的數字比如 17,31,63,127和129對于其他剩下的數字,在面對大量的哈希運算時,仍然有一個大大的優勢,就是這些數字能夠將乘法用位運算配合加減法來替換,這樣的運算速度會提高.畢竟一個好的哈希算法要求既有好的分布,也要有高的計算速度,能同時達到這兩點的數字很少.

php技術php-perl哈希算法實現(times33哈希算法),轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 蜜桃久久久亚洲精品成人 | 国产97精品久久久天天A片 | 宅男午夜大片又黄又爽大片 | 亚洲无码小格式 | 亚洲国产精品嫩草影院久久 | 成人AV无码一二二区视频免费看 | 国产精品免费一区二区三区视频 | 欧美亚洲精品午夜福利AV | 国产精品亚洲专一区二区三区 | 与嫂子同居的日子在线观看 | 欧美日韩综合一区 | 嗯啊…嗯np男男双性总受 | 久久妇女高潮几次MBA | 嫩草影院未满十八岁禁止入内 | 亚洲欧美一区二区三区久久 | 公和我做好爽添厨房中文字幕 | 色欲精品久久人妻AV中文字幕 | 伊人国产在线播放 | 99国产精品久久人妻无码 | 国产小视频国产精品 | 久久视频这里只精品99热在线观看 | 扒开女人下面使劲桶动态图 | 羞羞一区二区三区四区片 | 午夜亚洲国产理论片二级港台二级 | 亚洲国产精品嫩草影院 | 国产AV国片精品无套内谢无码 | 久久久黄色片 | 久久精选视频 | 永久免费看bbb| H揉捏娇喘乳叫床NP调教视频 | 全黄H全肉细节文NP 全黄h全肉细节全文 | 校园全肉高h湿一女多男 | 2012中文字幕在线动漫电影 | 成人免费公开视频 | 午夜无码片在线观看影院 | 欧美影院在线观看完整版 mp4 | 国产精品久久久久久久久免费下载 | 亚洲视频一区在线 | 天天躁人人躁人人躁狂躁 | ca88亚洲城娱乐 | 夜色88V精品国产亚洲AV |