青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

牽著老婆滿(mǎn)街逛

嚴(yán)以律己,寬以待人. 三思而后行.
GMail/GTalk: yanglinbo#google.com;
MSN/Email: tx7do#yahoo.com.cn;
QQ: 3 0 3 3 9 6 9 2 0 .

time33 哈希函數(shù),又叫 DJBX33A,Bernstein's hash

轉(zhuǎn)載自:http://www.cnblogs.com/napoleon_liu/articles/1911571.html

php, apache, perl, bsddb都使用time33哈希.

最簡(jiǎn)單的版本

  uint32_t time33(char const *str, int len) 
    

        unsigned 
long  hash = 0
        
for (int i = 0; i < len; i++
            hash 
= hash *33 + (unsigned long) str[i]; 
        }
 
        
return hash; 
    }

 

這個(gè)版本最可以體現(xiàn)time33的算法思路,夠簡(jiǎn)單。

 

把乘法操作換成位操作

        unsigned long time33(char const *str, int len) 
    

        unsigned 
long  hash = 0
        
for (int i = 0; i < len; i++
            hash 
= ((hash <<5+ hash) + (unsigned long) str[i]; 
        }
 
        
return hash; 
    }
 

59個(gè)字符1000 0000次運(yùn)行(gcc沒(méi)有開(kāi)啟優(yōu)化,因?yàn)殚_(kāi)了優(yōu)化后兩個(gè)函數(shù)的實(shí)際代碼會(huì)一樣)

第一個(gè):

real    0m4.389s 
user    0m4.388s 
sys     0m0.000s

 

第二個(gè):

real    0m4.137s 
user    0m4.120s 
sys     0m0.000s

 

 

gcc –O2優(yōu)化后是

real    0m1.367s 
user    0m1.360s 
sys     0m0.000s

 

 

php版本

 

inline unsigned time33(char const*str, int len) 

     unsigned long hash 
= 5381
     
/* variant with the hash unrolled eight times */ 
     
for (; len >= 8; len -= 8) { 
         hash 
= ((hash << 5+ hash) + *str++
         hash 
= ((hash << 5+ hash) + *str++
         hash 
= ((hash << 5+ hash) + *str++
         hash 
= ((hash << 5+ hash) + *str++
        hash 
= ((hash << 5+ hash) + *str++
        hash 
= ((hash << 5+ hash) + *str++
        hash 
= ((hash << 5+ hash) + *str++
        hash 
= ((hash << 5+ hash) + *str++
    } 
    
switch (len) { 
        
case 7: hash = ((hash << 5+ hash) + *str++/* fallthrough */ 
        
case 6: hash = ((hash << 5+ hash) + *str++/* fallthrough */ 
        
case 5: hash = ((hash << 5+ hash) + *str++/* fallthrough */ 
        
case 4: hash = ((hash << 5+ hash) + *str++/* fallthrough */ 
        
case 3: hash = ((hash << 5+ hash) + *str++/* fallthrough */ 
        
case 2: hash = ((hash << 5+ hash) + *str++/* fallthrough */ 
        
case 1: hash = ((hash << 5+ hash) + *str++break
        
case 0: break
    } 
    
return hash; 

 

59個(gè)字符,1000 0000次

real    0m1.088s 
user    0m1.068s 
sys     0m0.000s

 

速度提升主要在循環(huán)展開(kāi), 對(duì)于短字符,這個(gè)是不明顯的。

php版本的hash初始值是5381, 這個(gè)

 

Magic Constant 5381:

  
1. odd number

  
2. prime number

  
3. deficient number

  
4001/010/100/000/101 b

 

Apache版本

unsigned long time33(char const  *str, int *len) 

    unsigned 
long hash = 0;

    
const char *p=str; 
    
if (*len<=0
        
for(p = str; *p; p++
            hash 
= hash * 33 + *p; 
        }
 
        
*len = p - str; 
    }
 
    
else 
        
int i = *len; 
        
for (p = str;i; i--, p++
            hash 
= hash * 33 + *p; 
        }
 
    }
 
    
return hash; 
}

 

測(cè)試結(jié)果

real    0m1.418s 
user    0m1.412s 
sys     0m0.004s

 

 

綜上,我的改進(jìn)版本

#define likely(x) __builtin_expect((x),1) 
#define unlikely(x) __builtin_expect((x),0) 
    
//php版本 
    unsigned long time33(char const *str, int len=-1
    

        unsigned 
long hash = 5381
        
/* variant with the hash unrolled eight times */ 
        
char const *= str; 
        
if (unlikely(len<0)) 
                
for(; *p; p++
                    hash 
= hash * 33 + *p; 
                }
 
                
return hash; 
        }


#define TIME33_HASH_MIXED_CH()  hash = ((hash<<5)+hash) + *p++ 
        
for (; len >= 8; len -= 8
            TIME33_HASH_MIXED_CH(); 
//
            TIME33_HASH_MIXED_CH(); //
            TIME33_HASH_MIXED_CH(); //
            TIME33_HASH_MIXED_CH(); //
            TIME33_HASH_MIXED_CH(); //
            TIME33_HASH_MIXED_CH(); //
            TIME33_HASH_MIXED_CH(); //
            TIME33_HASH_MIXED_CH(); //
       }
 
       
switch (len) 
           
case 7: TIME33_HASH_MIXED_CH(); /* fallthrough */ 
           
case 6: TIME33_HASH_MIXED_CH(); /* fallthrough */ 
           
case 5: TIME33_HASH_MIXED_CH(); /* fallthrough */ 
           
case 4: TIME33_HASH_MIXED_CH(); /* fallthrough */ 
           
case 3: TIME33_HASH_MIXED_CH(); /* fallthrough */ 
           
case 2: TIME33_HASH_MIXED_CH(); /* fallthrough */ 
           
case 1: TIME33_HASH_MIXED_CH(); break
           
case 0break
       }
 
       
return hash; 
   }


#undef TIME33_HASH_MIXED_CH

 

測(cè)試結(jié)果

real    0m1.072s 
user    0m1.064s 
sys     0m0.000s

測(cè)試過(guò), 重復(fù)率在 1/2000。

 

為什么是33的倍數(shù), PHP中注釋是

DJBX33A (Daniel J. Bernstein, Times 33 with Addition)
  This 
is Daniel J. Bernstein's popular `times 33' hash function as
  posted by him years ago on comp.lang.c. It basically uses a function
  like ``hash(i) 
= hash(i-1* 33 + str[i]''. This is one of the best
  known hash functions 
for strings. Because it is both computed very
  fast and distributes very well.
  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 RSE did now) 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, the number 33 not
  even has the best value. But the number 
33 and a few other equally
  good numbers like 
173163127 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 and seems to be the reason why Daniel J.
  Bernstein also preferred it.
                   
-- Ralf S. Engelschall rse@engelschall.com

 

 

其它倍數(shù)

Ngix使用的是 time31

Tokyo Cabinet使用的是 time37

Bob在他的文章說(shuō),小寫(xiě)英文詞匯適合33, 大小寫(xiě)混合使用65。time33比較適合的是英文詞匯的hash.


posted on 2011-07-26 15:07 楊粼波 閱讀(1940) 評(píng)論(0)  編輯 收藏 引用


只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美一区二区三区免费视| 国产精品国产自产拍高清av王其| 久久午夜国产精品| 久久国产精品色婷婷| 午夜影院日韩| 久久九九国产| 米奇777在线欧美播放| 裸体歌舞表演一区二区| 麻豆精品视频在线| 亚洲第一福利在线观看| 欧美成人激情视频免费观看| 亚洲国产视频a| 亚洲精品在线视频| 亚洲欧美久久久| 久久免费高清视频| 欧美精品www在线观看| 国产精品劲爆视频| 精品9999| 在线一区视频| 国产精品你懂的在线欣赏| 国产精品人成在线观看免费| 狠狠色狠狠色综合日日五| 亚洲福利电影| 亚洲欧美视频在线观看| 老鸭窝亚洲一区二区三区| 欧美大片在线观看一区二区| 一区二区三区回区在观看免费视频| 亚洲欧美网站| 欧美激情1区2区3区| 国产精品日韩在线观看| 亚洲国产导航| 亚洲欧美中文字幕| 免费的成人av| 亚洲系列中文字幕| 欧美精品电影| 国内视频一区| 亚洲欧洲av一区二区三区久久| 麻豆精品在线播放| aa日韩免费精品视频一| 猛干欧美女孩| 国产日韩精品在线播放| 亚洲性视频h| 亚洲欧洲在线免费| 久久免费精品视频| 韩国av一区二区| 性欧美超级视频| 亚洲视频 欧洲视频| 欧美成人午夜77777| 精品av久久707| 久久久91精品| 久久av红桃一区二区小说| 欧美视频精品在线| 99re6热在线精品视频播放速度| 男女视频一区二区| 久久久久高清| 国产综合欧美| 久久免费黄色| 久久久人成影片一区二区三区观看| 国产精品一级| 午夜精品久久久久久久久久久久久| 亚洲精品一区二区三区婷婷月| 麻豆精品91| 亚洲精品123区| 亚洲国产欧美在线人成| 免费日韩成人| 亚洲精品字幕| 亚洲精品欧美专区| 欧美午夜精彩| 亚洲午夜视频在线观看| 亚洲视频在线二区| 国产欧美成人| 久久久久久久999精品视频| 欧美影视一区| 一区二区视频免费在线观看| 免费不卡亚洲欧美| 久久这里只有| 亚洲免费大片| 一区二区三区四区五区视频| 国产精品卡一卡二卡三| 国产欧美综合在线| 欧美在线视频免费观看| 性欧美8khd高清极品| 国模吧视频一区| 蜜桃伊人久久| 欧美日韩国产影片| 亚洲素人一区二区| 欧美在线一二三四区| 在线观看亚洲a| 亚洲区免费影片| 国产精品综合| 欧美激情一二区| 欧美三级日韩三级国产三级| 亚洲免费在线看| 久久久一区二区三区| 日韩视频不卡中文| 亚洲欧美精品中文字幕在线| 在线日韩av片| 一区二区三区视频在线播放| 狠狠色丁香久久综合频道| 亚洲国产欧美另类丝袜| 国产精品久久久久影院色老大 | 乱人伦精品视频在线观看| 99精品欧美一区二区三区综合在线| av不卡免费看| 亚洲激情网站| 99在线|亚洲一区二区| 国产自产精品| 在线亚洲国产精品网站| 亚洲电影有码| 性色av一区二区三区红粉影视| 日韩午夜在线| 久久久久久噜噜噜久久久精品| 一卡二卡3卡四卡高清精品视频| 欧美在线关看| 亚洲欧美经典视频| 嫩模写真一区二区三区三州| 久久精品亚洲精品| 国产精品女主播一区二区三区| 亚洲精品乱码久久久久久黑人| 亚洲成人在线免费| 欧美亚洲综合另类| 亚洲视频1区2区| 欧美精品成人91久久久久久久| 久久午夜精品一区二区| 国产精品日韩精品欧美在线| 亚洲久久一区| 亚洲人在线视频| 久久久亚洲精品一区二区三区| 欧美一区二区三区视频免费播放 | 亚洲国产成人av| 国产亚洲网站| 亚洲视频精选在线| 亚洲一区二区三区在线视频| 欧美日韩岛国| 亚洲国产高清高潮精品美女| 一区二区视频免费在线观看| 欧美一区二视频| 久久九九电影| 激情综合在线| 久久精品亚洲一区| 久久天堂成人| 亚洲欧美中文日韩v在线观看| 亚洲自拍16p| 国产精品豆花视频| 99精品视频免费观看| 一本色道久久综合亚洲精品小说| 久久一区激情| 欧美激情二区三区| 亚洲国产精品一区二区www在线 | 亚洲精品在线一区二区| 日韩视频免费在线观看| 欧美精品一区二区三区在线播放 | 欧美日韩在线看| 一本久道久久久| 午夜精品在线看| 国产乱码精品1区2区3区| 亚洲欧美日本国产有色| 欧美有码视频| 亚洲高清在线精品| 欧美日韩精品免费观看视一区二区| 一本色道久久88亚洲综合88| 午夜精彩视频在线观看不卡 | 久久嫩草精品久久久精品一| 国内精品嫩模av私拍在线观看 | 国产亚洲一区二区三区在线播放| 欧美中文字幕在线视频| 免费日韩av| 在线视频亚洲欧美| 国产精品一区二区黑丝| 久久视频一区| 亚洲天堂第二页| 久久综合五月天婷婷伊人| 日韩午夜中文字幕| 国产一区二区三区黄| 欧美激情视频在线免费观看 欧美视频免费一 | 欧美性事免费在线观看| 欧美专区在线| 91久久精品国产91性色| 久久精品人人做人人爽电影蜜月| 精品动漫3d一区二区三区免费| 欧美大尺度在线观看| 亚洲字幕一区二区| 亚洲国产岛国毛片在线| 午夜久久资源| 亚洲人成久久| 国产亚洲一级高清| 欧美日韩在线播放三区| 久久中文字幕一区| 久久亚洲欧美| 亚洲一区免费网站| 91久久精品一区| 国产亚洲综合性久久久影院| 欧美日韩免费在线视频| 免费不卡在线观看av| 欧美一级专区| 亚洲桃花岛网站| 日韩天堂在线观看| 亚洲国产mv| 亚洲第一网站| 欧美电影专区|