LoginSignup
7
4

More than 5 years have passed since last update.

繰り返しの性能の差が戦力の決定的な違いでない事を教えてやれない

Posted at

** 誤差だよそれは! **

ズンドコキヨシで動的型付言語のベンチマーク - Qiitaの処理を元に色々繰り返してみた。

実行環境

PHP 7.0.3 (cli)

比較処理そのものがオーバーヘッドになるっぽい

殆ど誤差の範囲ですが、なぜかforeachが最速だった。

"while < for"は腑に落ちる所。
forの場合は周回が終わった後にもう一個処理ブロックがあるしね。

gotoとwhileの差はほんとに判定処理そのものの差の予感。

foreach goto while for foreach2 foreach1
out 66.869176 66.95782 66.97618 66.981272 67.0028 73.343344
real 0m10.108s 0m11.247s 0m10.733s 0m10.954s 0m10.591s 0m11.565s
user 0m10.090s 0m11.231s 0m10.720s 0m10.935s 0m10.556s 0m11.551s
sys 0m0.013s 0m0.010s 0m0.006s 0m0.009s 0m0.009s 0m0.008s

ソース類

原点の "while"

while.php
<?php
$i = $total = 500000;
$length = 0;
while ($i--) {
    $z = 0;
    do {
        $length += 2;
        if (mt_rand() < 1073741824) {
            ++$z;
        } else {
            if ($z >= 4) break;
            $z = 0;
        }
    } while (true);
}
$out = $length / $total + 3;
echo "$out\n";

"for"

for.php
<?php
$i = $total = 500000;
$length = 0;
for (;$i--;) {
    $z = 0;
    for (;true;) {
        $length += 2;
        if (mt_rand() < 1073741824) {
            ++$z;
        } else {
            if ($z >= 4) break;
            $z = 0;
        }
    }
}
$out = $length / $total + 3;
echo "$out\n";

foreach

foreach:50万件一気に配列を作り、変数に入れる

foreach1.php
<?php
$i = $total = 500000;
$length = 0;
$range = range(0, $i);
foreach ($range as $i) {
    $z = 0;
    do {
        $length += 2;
        if (mt_rand() < 1073741824) {
            ++$z;
        } else {
            if ($z >= 4) break;
            $z = 0;
        }
    } while (true);
}
$out = $length / $total + 3;
echo "$out\n";

foreach:5万件の配列を作り、10回回す

foreach2.php
<?php
$i = $total = 500000;
$length = 0;
$range = range(0, 50000);
foreach (range(0, 10) as $n) {
    foreach ($range as $i) {
        $z = 0;
        do {
            $length += 2;
            if (mt_rand() < 1073741824) {
                ++$z;
            } else {
                if ($z >= 4) break;
                $z = 0;
            }
        } while (true);
    }
}
$out = $length / $total + 3;
echo "$out\n";

foreach:50万件の配列を作り、そのまま回す

foreach2.php
<?php
$i = $total = 500000;
$length = 0;
foreach (range(0, $i) as $i) {
    $z = 0;
    do {
        $length += 2;
        if (mt_rand() < 1073741824) {
            ++$z;
        } else {
            if ($z >= 4) break;
            $z = 0;
        }
    } while (true);
}
$out = $length / $total + 3;
echo "$out\n";

goto

goto.php
<?php
$i = $total = 500000;
$length = 0;
start:
    $z = 0;
    inner:
        $length += 2;
        if (mt_rand() < 1073741824) {
            ++$z;
        } else {
            if ($z >= 4) goto break_point;
            $z = 0;
        }
    goto inner;
    break_point:
if ($i--) {
    goto start;
}
$out = $length / $total + 3;
echo "$out\n";
7
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
4