Home > Article > Backend Development > PHP Hash algorithm: Times33 algorithm code example, hashtimes33_PHP tutorial
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
}
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".