PHP内核——hash算法

来源:互联网 发布:mac 无法访问只读文档 编辑:程序博客网 时间:2024/05/21 17:05

php的hash算法是采用典型的DJBX33A算法

原理:
hash(i) = hash(i-1)*33 + str[i]
hash(0) = 5381
php内部实现:

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 -= 8) {        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中采用的是Times33算法
算法思想跟DJBX33A基本一致

sub perlhash{    $hash = 0;    foreach(split){        $hash = $hash*33 + ord($_);    }    return hash;}

对比两个算法发现不同之处主要有两点:1、初始值2、移位与乘。显然DJB33A的效率要更高一点。对于php初始值为什么选择5381,可能是使用经验值吧~

0 0
原创粉丝点击