- 注册时间
- 2010-11-11
- 最后登录
- 2025-5-27
- 阅读权限
- 200
- 积分
- 14361
- 精华
- 2
- 帖子
- 843
  

TA的每日心情 | 无聊 2025-5-27 03:37:20 |
---|
签到天数: 366 天 [LV.9]以坛为家II
我玩的应用:
  
|
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().
|
|