Php-perl hashing algorithm implementation (TIMES33 hashing algorithm) _php tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
APR_DECLARE_NONSTD (unsigned int) apr_hashfunc_default (const char *char_key,
apr_ssize_t *klen)
{
unsigned int hash = 0;
Const unsigned char *key = (const unsigned char *) Char_key;
const unsigned char *p;
apr_ssize_t i;

/*
* This is the popular ' Times ' hash algorithm which are used by
* Perl and also appears in Berkeley DB. The one of the best
* Known hash functions for strings because it's both computed
* Very fast and distributes very well.
*
* The originator may be Dan Bernstein and the code in Berkeley DB
* cites Chris Torek as the source. The best citation I had found
* is ' Chris Torek, Hash function for text ' in C, Usenet message
* <27038@mimsy.umd.edu> in Comp.lang.c, October, 1990. ' In Rich
* Salz ' s USENIX 1992 paper about INN which can is found at
* .
*
* The magic of number, i.e. why it works better than many other
* constants, prime or not, have never been adequately explained by
* anyone. So I try a explanation:if one experimentally tests all
* Multipliers between 1 and writing a low-level
* Data Structure Library some time ago) one detects that even
* Numbers is not useable at all. The remaining odd numbers
* (except for the number 1) work more or less all equally well.
* They all distribute on an acceptable the and this is a hash
* Table with an average percent of approx. 86%.
*
* If One compares the chi^2 values of the variants (see
* Bob Jenkins ' Hashing frequently asked Questions ' at
* Http://burtleburtle.net/bob/hash/hashfaq.html for a description
* of chi^2), the number is not even have the best value. But the
* Number and a few other equally good numbers like 17, 31, 63,
* 127 and 129 has nevertheless a great advantage to the remaining
* Numbers in the large set of possible multipliers:their multiply
* operation can replaced by a faster operation based on just one
* Shift plus either a single addition or subtraction operation. and
* Because a hash function have to both distribute good _and_ have to
* Be very fast to compute, those few numbers should is preferred.
*
*--Ralf S. Engelschall
*/

if (*klen = = apr_hash_key_string) {
for (p = key; *p; p++) {
hash = hash * + *p;
}
*klen = P-key;
}
else {
for (p = key, i = *klen; i; I--, p++) {
hash = hash * + *p;
}
}
return hash;
}

translation of the function comment Section : This is a well-known times33 hashing algorithm, which is adopted by the Perl language and appears in Berkeley DB. It is one of the best hashing algorithms known, when processing a hash with a string as a key value, It has a very fast computational efficiency and a good hash distribution. The first proposed algorithm is Dan Bernstein, but the source code is actually made by Clris Torek in Berkeley Db. I found the most exact citation in this saying "Chris torek,c language text hash function, Usenet news <<27038@mimsy.umd.edu> in comp.lang.c, October 1990. " Mentioned in Rich Salz's article on a discussion inn published in the Usenix newspaper in 1992. This article can be found on the. 33 This wonderful figure, why does it work better than other values? No matter how important or not, no one has ever been able to fully explain the reasons. So here, let me try to explain. If someone is trying to test every number from 1 to 256 (as I wrote in a library of underlying data structures in the previous period), He will find that no number is particularly prominent. The 128 odd numbers (except 1) are all similar in performance and can achieve an acceptable hash distribution with an average distribution rate of about 86%. If you compare the variance values in these 128 odd numbers (Gibbon: Statistical terminology, which represents the average deviation between a random variable and its mathematical expectation) (see Bob Jenkins's <哈希常见疑问> http://burtleburtle.net/bob/hash/ hashfaq.html, a description of the squared difference), the number 33 is not the best performance. (Gibbon: Here, according to my understanding, according to the common sense, it should be the smaller the variance is stable, but because it is not clear that the author of the variance of the formula, as well as in the hash of the discrete table, is not the greater the better, so it is not known whether the performance of this is a large variance value or a small variance But the number 33 and some other equally good numbers such as 17,31,63,127 and 129 for the rest of the numbers, in the face of a large number of hashing, there is still a big advantage, is that these numbers can be multiplied with the bitwise operation with addition and subtraction to replace, This will increase the speed of the operation. After all, a good hashing algorithm requires both good distribution and high computational speed, and the number of these two points can be achieved at the same time.

http://www.bkjia.com/PHPjc/633589.html www.bkjia.com true http://www.bkjia.com/PHPjc/633589.html techarticle Copy the code code as follows: APR_DECLARE_NONSTD (unsigned int) apr_hashfunc_default (const char *char_key, apr_ssize_t *klen) {unsigned int hash = 0; Const unsigned char *key = (Const ...

  • 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.