首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么"hash('md5','string')“比"md5('string')”快?

为什么"hash('md5','string')“比"md5('string')”快?
EN

Stack Overflow用户
提问于 2013-03-19 20:51:06
回答 2查看 1.7K关注 0票数 12

http://www.php.net/manual/en/function.hash.php#73792上,它声明了一个测试,表明md5()函数比等效的hash()函数慢大约3倍。

如何解释这种差异?md5()函数有什么不同之处和/或更多?

EN

回答 2

Stack Overflow用户

发布于 2013-03-19 21:08:46

是100%正确的 ...也就是说,如果您仍在使用PHP早期版本的PHP,例如PHP 5.1.2PHP 5.2.2,那么在PHP的最新稳定版本中,它们是相同的,并且在某些版本中md5的运行速度会稍快一些

Here is a simple test in most PHP version

您还需要注意,基准方法是错误的,改变位置会影响结果…这就是如何获得更好的结果。

代码语言:javascript
复制
set_time_limit(0);
echo "<pre>";

function m1($total) {
    for($i = 0; $i < $total; $i ++)
        hash('md5', 'string');
}

function m2($total) {
    for($i = 0; $i < $total; $i ++)
        md5('string');
}

function m3($total) {
    for($i = 0; $i < $total; $i ++)
        hash('sha1', 'string');
}

function m4($total) {
    for($i = 0; $i < $total; $i ++)
        sha1('string');
}

function m5($total) {
    for($i = 0; $i < $total; $i ++)
        hash('md5', $i);
}

function m6($total) {
    for($i = 0; $i < $total; $i ++)
        md5($i);
}

function m7($total) {
    for($i = 0; $i < $total; $i ++)
        hash('sha1', $i);
}

function m8($total) {
    for($i = 0; $i < $total; $i ++)
        sha1($i);
}

$result = array(
        'm1' => 0,
        'm2' => 0,
        'm3' => 0,
        'm4' => 0,
        'm5' => 0,
        'm6' => 0,
        'm7' => 0,
        'm8' => 0
);

$total = 10000;

for($i = 0; $i < 100; ++ $i) {
    foreach ( array_keys($result) as $key ) {
        $alpha = microtime(true);
        $key($total);
        $result[$key] += microtime(true) - $alpha;
    }
}

echo '<pre>';
echo "Single Run\n";
print_r($result);
echo '</pre>';

输出

代码语言:javascript
复制
Single Run
Array
(
    [m1] => 0.58715152740479                 <--- hash/md5/string
    [m2] => 0.41520881652832                 <--- md5/string
    [m3] => 0.79592990875244                 <--- hash/sha1/string
    [m4] => 0.61766123771667                 <--- sha1/string
    [m5] => 0.67594528198242                 <--- hash/md5/$i
    [m6] => 0.51757597923279                 <--- md5/$i
    [m7] => 0.90692067146301                 <--- hash/sha1/$i
    [m8] => 0.74792814254761                 <--- sha1/$i

)

Live Test

票数 5
EN

Stack Overflow用户

发布于 2013-03-19 21:06:38

都是一样的!您需要使用大字符串对其进行测试才能检查它,我使用以下代码:

代码语言:javascript
复制
<?php

$s="";
for ($i=0;$i<1000000;$i++)
$s.=$i;
$time=microtime(1);
   hash('md5', $s);
echo microtime(1)-$time,': hash/md5<br>';

$time=microtime(1);

 md5($s);
echo microtime(1)-$time,': md5<br>';

$time=microtime(1);
hash('sha1', $s);
echo microtime(1)-$time,': hash/sha1<br>';

$time=microtime(1);
sha1($s);
echo microtime(1)-$time,': sha1<br>';
?>

这是我的结果:

代码语言:javascript
复制
0.015523910522461: hash/md5
0.01521897315979: md5
0.020196914672852: hash/sha1
0.020323038101196: sha1

非常相似!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15500026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档