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

13

可能是因为md5()hash("md5", )的别名,因此会减慢速度。但请不要引用我的话。 - EM-Creations
正如C所说,尝试在更大的数据上运行。在PHP中,每个别名都需要更多时间,因为解析器会进行两次搜索。第一次搜索别名,然后搜索原始函数。这是PHP的问题,但它允许不指定数据类型。 对于值转换,你也有同样的情况。(int)比intval()快得多。虽然它不是别名,但执行时间差异相当大。 - imclickingmaniac
1
哦,还有一件事。不要再使用MD5了。现在的MD5已经不安全了。使用更高级的SHA或类似于PHP5.5即将推出的其他方法。 - imclickingmaniac
2
@imclickingmaniac 一刀切的说法是伤人的。就像其他任何事情一样,如果你了解你所使用的工具的限制,那么你就不会有问题。 - Mike B
2
@imclickingmaniac 我正在使用它来创建哈希以进行查找,而不是为了安全 :) - Pim
显示剩余2条评论
2个回答

5
完全正确,如果您仍在使用早期版本的 PHP,例如PHP 5.1.2PHP 5.2.2,那么在大多数最新稳定版本的PHP中,它们是相同的,并且在某些版本中运行速度略快。 这里有一个简单的测试适用于大多数PHP版本
您还需要注意基准测试方法是错误的,并且改变位置会影响结果... 这是如何获得更好的结果。
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>';

输出

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


2

它们是相同的!!! 你需要使用大字符串来测试它,我使用了这段代码:

<?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>';
?>

这是我的结果:

0.015523910522461: hash/md5
0.01521897315979: md5
0.020196914672852: hash/sha1
0.020323038101196: sha1

非常相似!!!

啊,有趣!因为md5()是hash("md5", )的别名,所以对于小字符串的差异是由此引起的。 - Pim
1
那么...不是一个真正的问题? - Mike B
@Pim,对我来说似乎是这样 - One Man Crew
@Mike B 为什么不呢?这是一个真正的问题。 - One Man Crew
2
在解释型 PHP 上运行这些基准测试是毫无意义的。你使用的是 6 年前的同一版本 PHP 吗?你应该直接去源代码中解释哈希和 md5 之间的区别 https://github.com/php/php-src/blob/master/ext/hash/hash.c#L126。你已经证明了这个理论是错误的,但你还没有解释任何东西。 - Mike B

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接