文件上传要做哈希,当然是越快越好,文件哈希有sha1_file与md5_file。
哪个更快呢?网上也没搜到相关的文章,于是决定做一个小测试:
测试代码如下:
//哈希效率
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$timeS = microtime_float();
//$file = __file__;
$file = "E:\master.zip";
//$i = 100000;
$i = 100;
while($i--){
//$h = sha1_file($file);
$h = md5_file($file);
}
usleep(100);
$timeE = microtime_float();
echo ($timeE-$timeS);
这个脚本得到如下结果:
百万次1k大小文件
-------------------------------------------------------------------------------------------------------------------------------------------------------- s:
18.87629404068
19.195725822449
18.930956029892
18.881467962265
19.300680780411
m:
16.138952159882
16.400405073166
16.787165784836
16.40980591774
17.104665184021
百次
10M大小文件
---------------------------------------------------------------------------------------------------------------------------------------------------------
s:
11.082350873947
10.256709003448
12.991893911362
10.373815917969
11.834413194656
m:7.15002002716067.49447097778327.97997798919687.04725685119638.2962888717651
测试环境为64位win7 E5400 8G php5.2.17
看来,在这个环境下,MD5还是更快一点的。