foreach最強伝説

Cバリバリやってましたというオサーンがwhileeachを連発して「この方が見やすい」と謎なことをのたまうのでベンチマーク結果を投げつけました。

元ネタ→http://labs.cybozu.co.jp/blog/tsuruoka/anubis/blog_show/22:PEAR::Benchmarkでプログラムの実行時間を測定する

<?php
require_once 'Benchmark/Timer.php';
$timer = new Benchmark_Timer();
$timer->start();
$target_function = array(
    'bench_array_sum',
    'bench_for_loop',
    'bench_for_precount',
    'bench_while_loop',
    'bench_while_precount',
    'bench_while_currentshift',
    'bench_while_shift',
    'bench_while_next',
    'bench_foreach_loop',
    'bench_foreach_withkey',
    'bench_whileeach'
);
$target_sum = 0;
$target_array = array();
for($i = 0; $i < 4000; $i++) {
    $data = rand(1,10);
    $target_array[] = $data;
    $target_sum += $data;
}
$timer->setMarker('make_data');
foreach ($target_function as $function) {
    $result = $function($target_array);
    if ($result != $target_sum) {
        exit($function . " return invalid value\n");
    }
    $timer->setMarker($function);
}
$timer->stop();
$timer->display();
function bench_array_sum($target_array)
{
    return array_sum($target_array);
}
function bench_for_loop($target_array)
{
    $sum = 0;
    for ($i = 0; $i < count($target_array); $i++) {
        $sum += $target_array[$i];
    }
    return $sum;
}
function bench_for_precount($target_array)
{
    $sum = 0;
    $target_array_count = count($target_array);
    for ($i = 0; $i < $target_array_count; $i++) {
        $sum += $target_array[$i];
    }
    return $sum;
}
function bench_while_loop($target_array)
{
    $i = 0;
    $sum = 0;
    while ($i < count($target_array)) {
        $sum += $target_array[$i];
        $i++;
    }
    return $sum;
}
function bench_while_precount($target_array)
{
    $i = 0;
    $sum = 0;
    $target_array_count = count($target_array);
    while ($i < $target_array_count) {
        $sum += $target_array[$i];
        $i++;
    }
    return $sum;
}
function bench_while_shift($target_array)
{
    $sum = 0;
    while (NULL !== ($value = array_shift($target_array))) {
        $sum += $value;
    }
    return $sum;
}
function bench_while_currentshift($target_array)
{
    $sum = 0;
    while (current($target_array) !== false) {
        $sum += array_shift($target_array);
    }
    return $sum;
}
function bench_while_next($target_array)
{
    $sum = 0;
    $sum+= current($target_array);
    while (($value = next($target_array)) !== false) {
        $sum += $value;
    }
    return $sum;
}
function bench_foreach_loop($target_array)
{
    $sum = 0;
    foreach ($target_array as $value) {
        $sum += $value;
    }
    return $sum;
}
function bench_foreach_withkey($target_array)
{
    $sum = 0;
    foreach ($target_array as $key => $value) {
        $sum += $value;
    }
    return $sum;
}
function bench_whileeach($target_array)
{
    $sum = 0;
    while (list($key, $value) = each($target_array)) {
        $sum += $value;
    }
    return $sum;
}

Core 2 Duo T5500、1GBでphp5.1.6での結果。
xdebugは入ってますが、APCとかeAcceleratorとかハイカラなものは入ってません。

-----------------------------------------------------------------------
marker                     time index            ex time         perct
-----------------------------------------------------------------------
Start                      1210581808.81469700   -                0.00%
-----------------------------------------------------------------------
make_data                  1210581808.84215500   0.027458         5.89%
-----------------------------------------------------------------------
bench_array_sum            1210581808.84234800   0.000193         0.04%
-----------------------------------------------------------------------
bench_for_loop             1210581808.86441500   0.022067         4.73%
-----------------------------------------------------------------------
bench_for_precount         1210581808.86751600   0.003101         0.67%
-----------------------------------------------------------------------
bench_while_loop           1210581808.88944700   0.021931         4.71%
-----------------------------------------------------------------------
bench_while_precount       1210581808.89236800   0.002921         0.63%
-----------------------------------------------------------------------
bench_while_currentshift   1210581809.06808400   0.175716        37.70%
-----------------------------------------------------------------------
bench_while_shift          1210581809.22760800   0.159524        34.23%
-----------------------------------------------------------------------
bench_while_next           1210581809.24940900   0.021801         4.68%
-----------------------------------------------------------------------
bench_foreach_loop         1210581809.25163700   0.002228         0.48%
-----------------------------------------------------------------------
bench_foreach_withkey      1210581809.25413200   0.002495         0.54%
-----------------------------------------------------------------------
bench_whileeach            1210581809.28071000   0.026578         5.70%
-----------------------------------------------------------------------
Stop                       1210581809.28076400   0.000054         0.01%
-----------------------------------------------------------------------
total                      -                     0.466067       100.00%
-----------------------------------------------------------------------

foreach最強伝説。