色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

php的hash算法介紹

Hash Table是php的核心,這話一點(diǎn)都不過分。

php的數(shù)組,關(guān)聯(lián)數(shù)組,對象屬性,函數(shù)表,符號(hào)表,等等都是用HashTable來做為容器的。

php的HashTable采用的拉鏈法來解決沖突, 這個(gè)自不用多說, 我今天主要關(guān)注的就是php的Hash算法, 和這個(gè)算法本身透露出來的一些思想。

php的Hash采用的是目前最為普遍的DJBX33A (Daniel J. Bernstein, Times 33 with Addition), 這個(gè)算法被廣泛運(yùn)用與多個(gè)軟件項(xiàng)目,Apache, Perl和Berkeley DB等. 對于字符串而言這是目前所知道的最好的哈希算法,原因在于該算法的速度非常快,而且分類非常好(沖突小,分布均勻).

算法的核心思想就是:
復(fù)制代碼 代碼如下:
hash(i) = hash(i-1) * 33 + str[i]

在zend_hash.h中,我們可以找到在php中的這個(gè)算法:

復(fù)制代碼 代碼如下:
static inline ulong zend_inline_hash_func(char *arKey, uint nKeyLength)
{
    register ulong hash = 5381;

    /* variant with the hash unrolled eight times */
    for (; nKeyLength >= 8; nKeyLength -=  {
        hash = ((hash << 5) + hash) + *arKey++;
        hash = ((hash << 5) + hash) + *arKey++;
        hash = ((hash << 5) + hash) + *arKey++;
        hash = ((hash << 5) + hash) + *arKey++;
        hash = ((hash << 5) + hash) + *arKey++;
        hash = ((hash << 5) + hash) + *arKey++;
        hash = ((hash << 5) + hash) + *arKey++;
        hash = ((hash << 5) + hash) + *arKey++;
    }
    switch (nKeyLength) {
        case 7: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
        case 6: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
        case 5: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
        case 4: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
        case 3: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
        case 2: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */
        case 1: hash = ((hash << 5) + hash) + *arKey++; break;
        case 0: break;
EMPTY_SWITCH_DEFAULT_CASE()
    }
    return hash;
}

相比在Apache和Perl中直接采用的經(jīng)典Times 33算法:

復(fù)制代碼 代碼如下:
hashing function used in Perl 5.005:
  # Return the hashed value of a string: $hash = perlhash("key")
  # (Defined by the PERL_HASH macro in hv.h)
  sub perlhash
  {
      $hash = 0;
      foreach (split //, shift) {
          $hash = $hash*33 + ord($_);
      }
      return $hash;
  }

php的hash算法中, 我們可以看出很處細(xì)致的不同.

首先, 最不一樣的就是, php中并沒有使用直接乘33, 而是采用了:
復(fù)制代碼 代碼如下:
hash << 5 + hash

這樣當(dāng)然會(huì)比用乘快了.

然后, 特別要主意的就是使用的unrolled, 我前幾天看過一片文章講Discuz的緩存機(jī)制, 其中就有一條說是Discuz會(huì)根據(jù)帖子的熱度不同采用不同的緩存策略, 根據(jù)用戶習(xí)慣,而只緩存帖子的第一頁(因?yàn)楹苌儆腥藭?huì)翻帖子).

于此類似的思想, php鼓勵(lì)8位一下的字符索引, 他以8為單位使用unrolled來提高效率, 這不得不說也是個(gè)很細(xì)節(jié)的,很細(xì)致的地方.

另外還有inline, register變量 … 可以看出php的開發(fā)者在hash的優(yōu)化上也是煞費(fèi)苦心

最后就是, hash的初始值設(shè)置成了5381, 相比在Apache中的times算法和Perl中的Hash算法(都采用初始hash為0), 為什么選5381呢? 具體的原因我也不知道, 但是我發(fā)現(xiàn)了5381的一些特性:

復(fù)制代碼 代碼如下:
Magic Constant 5381:
1. odd number
2. prime number
3. deficient number

看了這些, 我有理由相信這個(gè)初始值的選定能提供更好的分類.

php技術(shù)php的hash算法介紹,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 前郭尔| 德庆县| 怀安县| 武威市| 武强县| 新昌县| 新巴尔虎左旗| 满洲里市| 怀宁县| 宾川县| 即墨市| 彩票| 恩平市| 渭南市| 武穴市| 林芝县| 合肥市| 遵义县| 英超| 扶风县| 观塘区| 普兰县| 周至县| 镇巴县| 大竹县| 济源市| 崇明县| 济宁市| 耒阳市| 鹤山市| 高清| 白银市| 章丘市| 航空| 宜都市| 安阳市| 青海省| 兴海县| 无极县| 观塘区| 万荣县|