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

牽著老婆滿(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>
            久久久噜噜噜久久中文字幕色伊伊| 尤物网精品视频| 农村妇女精品| 性欧美大战久久久久久久免费观看| 亚洲国产欧美另类丝袜| 久久久女女女女999久久| 亚洲一区二区三区四区五区黄| 亚洲国产美女精品久久久久∴| 国产日韩欧美一区二区三区在线观看 | 麻豆乱码国产一区二区三区| 亚洲性感美女99在线| 亚洲国产日本| 欧美电影免费观看网站| 久久久久国产精品一区二区| 先锋影音国产一区| 午夜精品久久99蜜桃的功能介绍| 日韩一区二区久久| 亚洲精品一线二线三线无人区| 伊人成综合网伊人222| 黄色日韩在线| 一区二区视频免费在线观看| 国产一区观看| 韩国av一区二区三区| 国产一区免费视频| 国产在线精品一区二区中文| 国产欧美一区二区视频| 国产精品天美传媒入口| 国产精品色在线| 国产精品久久久久999| 欧美性猛交一区二区三区精品| 欧美日韩亚洲不卡| 国产精品多人| 国产精品专区第二| 国产午夜亚洲精品理论片色戒| 国产视频在线一区二区| 狠狠狠色丁香婷婷综合久久五月| 国内精品视频666| 在线国产亚洲欧美| 亚洲精选一区二区| 亚洲午夜视频在线观看| 午夜精品久久久久久久| 久久精品国产清高在天天线| 久久久视频精品| 欧美大片18| 日韩视频免费看| 午夜精品视频一区| 久久久国产91| 欧美激情一区二区三级高清视频| 欧美日韩国产999| 99视频精品| 亚洲综合丁香| 久久免费视频网| 欧美精品尤物在线| 国产精品久久一区二区三区| 国产午夜精品全部视频在线播放| 精品福利电影| 一本久久综合亚洲鲁鲁| 性8sex亚洲区入口| 免费观看一级特黄欧美大片| 亚洲欧洲日本mm| 亚洲欧美日韩国产一区二区| 久久久人成影片一区二区三区观看| 美女精品自拍一二三四| 欧美日本在线| 国产在线不卡精品| 91久久久亚洲精品| 欧美一区二区在线免费播放| 欧美本精品男人aⅴ天堂| 日韩一区二区免费高清| 欧美一区二区三区视频| 女仆av观看一区| 国产精品网站视频| 亚洲精品久久久久久下一站 | 欧美jizzhd精品欧美巨大免费| 亚洲国产欧美日韩| 欧美在线视频一区二区三区| 欧美激情国产精品| 国产欧美va欧美不卡在线| 亚洲国产精品免费| 欧美亚洲一区在线| 亚洲人成网站777色婷婷| 午夜在线不卡| 欧美日韩国产综合网| 黄色成人在线网站| 午夜在线观看免费一区| 亚洲黄色免费网站| 久久久久久亚洲精品不卡4k岛国| 欧美午夜视频在线| 亚洲激情中文1区| 久久久久综合网| 亚洲天堂av高清| 欧美理论视频| 亚洲动漫精品| 久久久蜜桃一区二区人| 亚洲在线观看视频网站| 欧美精品一区二区视频| 亚洲第一色中文字幕| 欧美在线www| 在线亚洲激情| 欧美日韩国产亚洲一区| 亚洲日本在线观看| 欧美电影免费观看大全| 久久精品人人爽| 国产小视频国产精品| 午夜精品免费视频| 亚洲图色在线| 欧美性色aⅴ视频一区日韩精品| 亚洲美女黄色片| 亚洲国产另类精品专区| 久久久五月婷婷| 精品999在线播放| 久久一区二区三区国产精品| 亚洲欧美另类在线观看| 国产精品久久久久久av下载红粉 | 欧美怡红院视频| 国产日韩欧美二区| 久久成人精品电影| 香蕉成人啪国产精品视频综合网| 国产精品日韩专区| 性色av一区二区三区在线观看| 中文欧美在线视频| 国产精品国产成人国产三级| 亚洲视频专区在线| 在线中文字幕一区| 国产精品你懂的在线| 午夜精品一区二区在线观看| 亚洲婷婷在线| 国产日韩在线一区二区三区| 欧美一区二区视频免费观看| 亚洲欧美日韩区| 国内精品国语自产拍在线观看| 久久久久欧美精品| 久久深夜福利| 亚洲美女毛片| 夜夜嗨av一区二区三区网站四季av | 91久久国产综合久久蜜月精品| 欧美成人r级一区二区三区| 久久一区中文字幕| 亚洲精品在线三区| 亚洲最新视频在线播放| 国产精品美女久久久久av超清| 性色av一区二区三区| 欧美在线免费视屏| 亚洲电影第1页| 亚洲狠狠婷婷| 国产精品高精视频免费| 欧美中文字幕在线| 久久综合色综合88| 国产精品99久久久久久有的能看| 亚洲素人在线| 国内精品久久久久久| 欧美黑人在线观看| 欧美视频精品在线| 欧美日韩亚洲一区三区| 亚洲视频视频在线| 久久www免费人成看片高清 | 欧美一区二区三区在线观看视频 | 国产伦精品一区二区三区免费迷| 久久久久久成人| 欧美国产亚洲精品久久久8v| 亚洲女同同性videoxma| 欧美在线观看视频在线| 亚洲精品美女免费| 亚洲欧美日韩国产成人| 1024日韩| 亚洲无线一线二线三线区别av| 国产一区香蕉久久| 亚洲乱亚洲高清| 韩国一区电影| 亚洲美女视频在线观看| 国模一区二区三区| 亚洲国产黄色| 国产午夜精品视频| 99热在线精品观看| 精品福利av| 亚洲一本视频| 亚洲精品视频一区| 欧美在线免费| 亚洲男人av电影| 免费看的黄色欧美网站| 性欧美1819sex性高清| 欧美成人一区二区三区在线观看 | 日韩视频不卡| 在线看欧美视频| 午夜日韩电影| 一区二区三区高清不卡| 久久久久.com| 亚洲欧美中文日韩v在线观看| 久色成人在线| 久久视频一区| 国产精品久久久久久久久久久久久| 欧美成人精品不卡视频在线观看| 国产精品久久网站| 亚洲精品久久久久久久久| 激情丁香综合| 午夜精品久久久久久99热软件| 欧美国产视频一区二区| 久久中文字幕一区| 国产欧美日本一区视频| 日韩一区二区精品|