我基本上是在准备要放入数据库的短语,它们可能格式不正确,所以我想存储它们的简短哈希(我将简单地比较它们是否存在,所以哈希是理想的)。
我假设 MD5 在 100,000 多个请求上相当慢,所以我想知道散列短语的最佳方法是什么,也许推出我自己的散列函数或使用 hash('md4', '.. .'
最终会更快吗?
我知道 MySQL 有 MD5(),所以这将在查询端补充一点速度,但也许 MySQL 中还有一个更快的散列函数,我不知道它是否适用于 PHP..
最佳答案
fcn time generated hash
crc32: 0.03163 798740135
md5: 0.0731 0dbab6d0c841278d33be207f14eeab8b
sha1: 0.07331 417a9e5c9ac7c52e32727cfd25da99eca9339a80
xor: 0.65218 119
xor2: 0.29301 134217728
add: 0.57841 1105
而用来生成这个的代码是:
$loops = 100000;
$str = "ana are mere";
echo "<pre>";
$tss = microtime(true);
for($i=0; $i<$loops; $i++){
$x = crc32($str);
}
$tse = microtime(true);
echo "\ncrc32: \t" . round($tse-$tss, 5) . " \t" . $x;
$tss = microtime(true);
for($i=0; $i<$loops; $i++){
$x = md5($str);
}
$tse = microtime(true);
echo "\nmd5: \t".round($tse-$tss, 5) . " \t" . $x;
$tss = microtime(true);
for($i=0; $i<$loops; $i++){
$x = sha1($str);
}
$tse = microtime(true);
echo "\nsha1: \t".round($tse-$tss, 5) . " \t" . $x;
$tss = microtime(true);
for($i=0; $i<$loops; $i++){
$l = strlen($str);
$x = 0x77;
for($j=0;$j<$l;$j++){
$x = $x xor ord($str[$j]);
}
}
$tse = microtime(true);
echo "\nxor: \t".round($tse-$tss, 5) . " \t" . $x;
$tss = microtime(true);
for($i=0; $i<$loops; $i++){
$l = strlen($str);
$x = 0x08;
for($j=0;$j<$l;$j++){
$x = ($x<<2) xor $str[$j];
}
}
$tse = microtime(true);
echo "\nxor2: \t".round($tse-$tss, 5) . " \t" . $x;
$tss = microtime(true);
for($i=0; $i<$loops; $i++){
$l = strlen($str);
$x = 0;
for($j=0;$j<$l;$j++){
$x = $x + ord($str[$j]);
}
}
$tse = microtime(true);
echo "\nadd: \t".round($tse-$tss, 5) . " \t" . $x;
关于php - 非加密用途的最快哈希?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3665247/