md5()效率问题要注意
md5()函数的执行时间会随着字符串的长度增加而直线变慢。我们产生size长的随机字符串,并做md5()函数运算,代码如下:
function random($length, $numeric = 0) {
PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
if($numeric) {
$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
} else {
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
for($i = 0; $i < $length; $i++) {
$hash .= $chars;
}
}
return $hash;
}
function md5test($size) {
$str = random($size);
$mtime = explode(' ', microtime());
$stime = $mtime + $mtime;
md5($str);
$mtime = explode(' ', microtime());
echo number_format(($mtime + $mtime - $stime), 6)."<br>";
}
echo md5test(100);
echo md5test(1000);
echo md5test(10000);
echo md5test(100000);
结果是
0.000056
0.000047
0.000197
0.001964
如果需要对数据进行md5()运算,可以考虑对数据先做有损计算在md5().
页:
[1]