Home > php教程 > php手册 > PHP中sha1

PHP中sha1

WBOY
Release: 2016-06-06 19:49:05
Original
900 people have browsed it

文件上传要做哈希,当然是越快越好,文件哈希有sha1_file与md5_file。 哪个更快呢?网上也没搜到相关的文章,于是决定做一个小测试: 测试代码如下: //哈希效率function microtime_float(){ list($usec, $sec) = explode( , microtime()); return ((float)$

文件上传要做哈希,当然是越快越好,文件哈希有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);
Copy after login

这个脚本得到如下结果:

百万次1k大小文件
--------------------------------------------------------------------------------------------------------------------------------------------------------

s:18.87629404068 19.19572582244918.93095602989218.88146796226519.300680780411
m:16.13895215988216.40040507316616.78716578483616.40980591774 17.104665184021


百次10M大小文件
---------------------------------------------------------------------------------------------------------------------------------------------------------
s:11.08235087394710.25670900344812.99189391136210.37381591796911.834413194656

m:7.15002002716067.49447097778327.97997798919687.04725685119638.2962888717651


测试环境为64位win7 E5400 8G php5.2.17

看来,在这个环境下,MD5还是更快一点的。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template