12

http://www.php.net/manual/en/function.hash.php#73792md5()では、関数が同等の関数よりも約 3 倍遅いことを示すテストが記載されていhash()ます。

この違いを説明できるものは何ですか? 関数は何がmd5()違うか、またはそれ以上のことをしますか?

4

2 に答える 2

5

はい 100% 正しいです...つまり、PHP の初期バージョンをまだ使用している場合、再送信PHPPHP 5.1.2PHP 5.2.2た最新の安定バージョンではPHPそれらは同じでありmd5、一部のバージョンではわずかに高速に実行されます

これは、ほとんどの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

)

ライブテスト

于 2013-03-19T13:08:46.397 に答える
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

とても似ている!!!

于 2013-03-19T13:06:37.997 に答える