查看文章
 
md5()效率问题要注意
2007-09-24 16:31

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[mt_rand(0, $max)];
   }
}
return $hash;
}

function md5test($size) {
$str = random($size);
$mtime = explode(' ', microtime());
$stime = $mtime[1] + $mtime[0];

md5($str);

$mtime = explode(' ', microtime());
echo number_format(($mtime[1] + $mtime[0] - $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().


类别:Php||添加到搜藏 |分享到i贴吧|浏览(1450)|评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
     

   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu