PHP Hash algorithm: Times33 algorithm code example, hashtimes33_PHP tutorial

WBOY
Release: 2016-07-13 09:53:54
Original
1098 people have browsed it

PHP Hash algorithm: Times33 algorithm code example, hashtimes33

I recently read a book, which mentioned some Hash algorithms. What impressed me most was Times33. I didn’t understand it very well at the time, so I wrote a program today to verify it.
First enter the code:
Copy code The code is as follows:

/**
 * CRC32 Hash function
 * @param $str
 * @return int
 */
function hash32($str)
{
Return crc32($str) >> 16 & 0x7FFFFFFF;
}

/**
 * Times33 Hash function
 * @param $str
 * @return int
 */
function hash33($str)
{
$hash = 0;
for($i=0; $i           $hash = 33 * $hash ord($str{$i});
}
Return $hash & 0x7FFFFFFF;
}


$n = 10;

//Test Case 1
$stat = array();
for($i=0; $i<10000; $i ){
$str = substr(md5(microtime(true)), 0, 8);
$p = hash32($str) % $n;
If(isset($stat[$p])){
$stat[$p] ;
}else{
         $stat[$p] = 1;
}
}
print_r($stat);

//Test Case 2
$stat = array();
for($i=0; $i<10000; $i ){
$str = substr(md5(microtime(true)), 0, 8);
$p = hash33($str) % $n;
If(isset($stat[$p])){
$stat[$p] ;
}else{
         $stat[$p] = 1;
}
}
print_r($stat);

There are two test cases above. The first one uses the CRC32 method; the second one is the Times33 algorithm implementation.

Effect:

The result distribution is similar between the two algorithms (it is probably a problem with the data source, md5 only has 0-f). There are also articles saying that CRC32 is more evenly distributed (reference link:)
But it is time consuming, CRC32 is nearly twice as fast as Times33.

Why 33?

It is a prime number (prime number) and an odd number. In addition to 33, there are 131, 1313, 5381, etc. PHP's built-in Hash function uses 5381, which was also mentioned in a blog post by "Brother Bird".

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/998814.htmlTechArticlePHP Hash algorithm: Times33 algorithm code example, hashtimes33 I recently read a book, which mentioned some Hash algorithms. What impressed me more was Times33. I didn’t understand it very well at the time. Today I wrote a paragraph...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template