Time33 hash function, also called djbx33a, Bernstein's hash function

Source: Internet
Author: User

PHP, Apache, Perl, and bsddb all use time33 hash.

<Br/> inline unsigned time33 (char const * STR, int Len) <br/>{< br/> unsigned long hash = 5381; <br/>/* variant with the hash unrolled eight times */<br/> for (; Len> = 8; Len-= 8) {<br/> hash = (hash <5) + hash) + * STR ++; <br/> hash = (hash <5) + hash) + * STR ++; <br/> hash = (hash <5) + hash) + * STR ++; <br/> hash = (hash <5) + hash) + * STR ++; <br/> hash = (hash <5) + hash) + * STR ++; <br/> hash = (hash <5) + hash) + * STR ++; <br/> hash = (hash <5) + hash) + * STR ++; <br/> hash = (hash <5) + hash) + * STR ++; <br/>}< br/> switch (LEN) {<br/> case 7: Hash = (hash <5) + hash) + * STR ++;/* fallthrough... */<br/> case 6: Hash = (hash <5) + hash) + * STR ++;/* fallthrough... */<br/> case 5: Hash = (hash <5) + hash) + * STR ++;/* fallthrough... */<br/> case 4: Hash = (hash <5) + hash) + * STR ++;/* fallthrough... */<br/> case 3: Hash = (hash <5) + hash) + * STR ++;/* fallthrough... */<br/> case 2: Hash = (hash <5) + hash) + * STR ++;/* fallthrough... */<br/> case 1: Hash = (hash <5) + hash) + * STR ++; break; <br/> case 0: break; <br/>}< br/> return hash; <br/>} 
Simplest version

Uint32_t time33 (char const * STR, int Len) <br/>{< br/> unsigned long hash = 0; <br/> for (INT I = 0; I <Len; I ++) {<br/> hash = hash * 33 + (unsigned long) STR [I]; <br/>}< br/> return hash; <br/>}
This version is simple enough to reflect the algorithm idea of time33.

Replace a multiplication operation with a bitwise operation.
Unsigned long time33 (char const * STR, int Len) <br/>{< br/> unsigned long hash = 0; <br/> for (INT I = 0; I <Len; I ++) {<br/> hash = (hash <5) + hash) + (unsigned long) STR [I]; <br/>}< br/> return hash; <br/>}
59 characters 1000 0000 run (GCC does not enable optimization, because the actual code of the two functions will be the same after optimization)

First:

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

Second:

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

After gcc-O2 is optimized

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

PHP version

59 characters, 1000 0000 times

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

The speed increase is mainly in loop expansion. For short characters, This is not obvious.

In PHP, the initial hash value is 5381.

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) <br/>{< br/> unsigned long hash = 0; </P> <p> const char * P = STR; <br/> If (* Len <= 0) {<br/> for (P = STR; * P; P ++) {<br/> hash = hash * 33 + * P; <br/>}< br/> * Len = p-STR; <br/>}< br/> else {<br/> int I = * Len; <br/> for (P = STR; I --, P ++) {<br/> hash = hash * 33 + * P; <br/>}< br/> return hash; <br/>}

Test Results

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

In summary, my improved version

# Define likely (x) _ builtin_exact CT (x), 1) <br/> # define unlikely (x) _ builtin_exact CT (x), 0) <br/> // PHP version <br/> unsigned long time33 (char const * STR, int Len =-1) <br/>{< br/> unsigned long hash = 5381; <br/>/* variant with the hash unrolled eight times */<br/> char const * P = STR; <br/> If (unlikely (LEN <0 )) {<br/> for (; * P; P ++) {<br/> hash = hash * 33 + * P; <br/>}< br/> return hash; <br/>}</P> <p> # define time33_hash_mixed_ch () hash = (hash <5) + hash) + * P ++ <br/> for (; Len> = 8; Len-= 8) {<br/> time33_hash_mixed_ch (); // 1 <br/> time33_hash_mixed_ch (); // 2 <br/> time33_hash_mixed_ch (); // 3 <br/> time33_hash_mixed_ch (); // 4 <br/> time33_hash_mixed_ch (); // 5 <br/> time33_hash_mixed_ch (); // 6 <br/> time33_hash_mixed_ch (); // 7 <br/> time33_hash_mixed_ch (); // 8 <br/>}< br/> switch (LEN) {<br/> case 7: time33_hash_mixed_ch (); /* fallthrough... */<br/> case 6: time33_hash_mixed_ch ();/* fallthrough... */<br/> case 5: time33_hash_mixed_ch ();/* fallthrough... */<br/> case 4: time33_hash_mixed_ch ();/* fallthrough... */<br/> case 3: time33_hash_mixed_ch ();/* fallthrough... */<br/> case 2: time33_hash_mixed_ch ();/* fallthrough... */<br/> case 1: time33_hash_mixed_ch (); break; <br/> case 0: break; <br/>}< br/> return hash; <br/>}

# UNDEF time33_hash_mixed_ch
Test Results

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

Tested, with a repetition rate of 1/2000.

Why is it a multiple of 33? The comment in PHP is
Djbx33a (Daniel J. Bernstein, times 33 with addition)
This is Daniel J. Bernstein's popular 'times 33' hash function
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 extends other
Constants, prime or not, has never been adequately explained
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
(Could t 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 17, 31, 63,127 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 shoshould be preferred and seems to be the reason why Daniel J.
Bernstein also preferred it.
-- Ralf S. engelschall rse@engelschall.com
Other multiples
Ngix uses time31
Tokyo cabinet uses time37

Bob said in his article that lowercase English words are suitable for 33 words and 65 words are used in combination. Time33 is suitable for English vocabulary hash.

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.