国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php的hash算法介紹

2024-05-04 23:21:12
字體:
供稿:網(wǎng)友

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

PHP的數(shù)組,關(guān)聯(lián)數(shù)組,對(duì)象屬性,函數(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等. 對(duì)于字符串而言這是目前所知道的最好的哈希算法,原因在于該算法的速度非???,而且分類非常好(沖突小,分布均勻).

算法的核心思想就是:

復(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è)初始值的選定能提供更好的分類.

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 聂荣县| 五寨县| 汤原县| 前郭尔| 怀安县| 通渭县| 新昌县| 博野县| 永丰县| 南漳县| 博爱县| 商都县| 盐城市| 长春市| 轮台县| 加查县| 启东市| 大余县| 昭觉县| 长阳| 平和县| 且末县| 怀化市| 卓资县| 嘉兴市| 洞口县| 栾城县| 如皋市| 田东县| 石屏县| 四会市| 登封市| 张北县| 揭东县| 抚远县| 泗水县| 梁河县| 临潭县| 重庆市| 西昌市| 林州市|