Implementation principle of djbx33a (time33 hash)

Source: Internet
Author: User
Tags hash
Original address: http://www.cnblogs.com/napoleon_liu/articles/1911571.html Reference: Http://www.laruence.com/2009/07/23/994.html
time33 hash function, also called Djbx33a,bernstein ' s hash

PHP, Apache, Perl, bsddb all use the time33 hash. the simplest version

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;
}

This version of the most can embody time33 algorithm ideas, simple enough.

Replace the multiplication operation with a bitwise operation 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 characters 1000 0000 runs (GCC does not turn on optimizations because the actual code of the two functions is the same after the optimizations are turned on)

The first one:

Real 0m4.389s
User 0m4.388s
SYS 0m0.000s

The second one:

Real 0m4.137s
User 0m4.120s
SYS 0m0.000s

Gcc–o2 after optimization is

Real 0m1.367s
User 0m1.360s
SYS 0m0.000s

PHP Version

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 characters, 1000 0000 Times

Real 0m1.088s
User 0m1.068s
SYS 0m0.000s

Speed increase is mainly in the circulation, for short characters, this is not obvious.

PHP version of the hash initial value is 5381, this

Magic Constant 5381:

1. Odd number

2. Prime number

3. Deficient number

4.001/010/100/000/101 b Apache version

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 * + *p;
}
*len = P-str;
}
else {
int i = *len;
for (p = str;i I--, p++) {
hash = hash * + *p;
}
}
return hash;
}

Test results

Real 0m1.418s
User 0m1.412s
SYS 0m0.004s

on top of that, my improved version

#define LIKELY (x) __builtin_expect ((x), 1)
#define UNLIKELY (x) __builtin_expect ((x), 0)
PHP version
unsigned long time33 (char const *STR, int len=-1)
{
unsigned long hash = 5381;
/* Variant with the hash unrolled eight times * *
char const *P = str;
if (unlikely (len<0)) {
for (; *p; p++) {
hash = hash * + *p;
}
return hash;
}

#define TIME33_HASH_MIXED_CH () HASH = ((hash<<5) +hash) + *p++
for (; Len >= 8; len-= 8) {
Time33_hash_mixed_ch (); 1
Time33_hash_mixed_ch (); 2
Time33_hash_mixed_ch (); 3
Time33_hash_mixed_ch (); 4
Time33_hash_mixed_ch (); 5
Time33_hash_mixed_ch (); 6
Time33_hash_mixed_ch (); 7
Time33_hash_mixed_ch (); 8
}
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 0:break;
}
return hash;
}

#undef time33_hash_mixed_ch Test Results

Real 0m1.072s
User 0m1.064s
SYS 0m0.000s

Tested, repeat rate at 1/2000.

Why is a multiple of 33, the comment in PHP is

djbx33a (Daniel J. Bernstein, Times of addition)   This is Daniel J. Bernstein ' s popular ' times ' hash function as  posted by him years ago on comp.lang.c. It basically uses a function  like ' hash (i) = hash (i-1) * + 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-i.e. why it works bet ter than many other  constants, prime or not, has never the been adequately explained by . So I try a explanation:if one experimentally tests all  multipliers between 1 and 256 (as RSE did now) one detects That even  numbers are is not useable in all. The remaining 128 odd numbers  (except for the number 1) work more or less all equally. they  all distribute in the acceptable way and this way fill a hash table  with a average percent of approx. 86% .  If One compares the chi^2 values of the variants, the numbernot  even has the best value. But the number and a few other equally  good numbers, 127 and 129 have nevertheless a great  Advantage to the remaining numbers on large set of possible  multipliers:their multiply operation can be replace D by a faster  operation based to just one shift plus either a single addition  or subtraction operation. And because a hash function has to both  distribute good _and_ has to is very fast to compute, those few  s should be preferred and seems to be the reason why Daniel j.  Bernstein also preferred nbsp;             --Ralf S. Engelschall Rse@engelschall.com Other Multiples Ngix is using Time31. Tokyo cabinet is using Time37.

Bob in his article says that the lowercase English vocabulary fits 33, and the case mix uses 65. Time33 is more suitable for the hash of English vocabulary.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.