Пример #1
0
}
function printRes($for, $foreach, $while, $doWhile)
{
    unset($for[0], $while[0], $foreach[0], $doWhile[0]);
    $whileAv = array_sum($while) / sizeof($while);
    $doWhileAv = array_sum($doWhile) / sizeof($doWhile);
    $forAv = array_sum($for) / sizeof($for);
    $foreachAv = array_sum($foreach) / sizeof($foreach);
    $max = max($forAv, $foreachAv, $whileAv, $doWhileAv);
    $data = "<h2>От каждого елемена отнимаем 15, 100000 елементов масива</h2>\n" . 'test while - %' . $whileAv / $max * 100 . ' time: ' . $whileAv . "<br>\n" . 'test Do while - %' . $doWhileAv / $max * 100 . ' time: ' . $doWhileAv . "<br>\n" . 'test for - %' . $forAv / $max * 100 . ' time: ' . $forAv . "<br>\n" . 'test foreach - %' . $foreachAv / $max * 100 . ' time: ' . $foreachAv . "<br>\n";
    echo $data;
    $path = 'test---1Array(100000)_' . date('d_hms') . ".txt";
    //var_dump($path);
    file_put_contents($path, $data, FILE_APPEND);
}
//$testArr1 = createArr(1000);
//$testArr1 = createArr(10000);
$testArr1 = createArr(100000);
?>
<h1> Тест скорости циклов </h1>

<?php 
for ($j = 1; $j <= 2; $j++) {
    for ($i = 0; $i <= 10; $i++) {
        $foreach[$i] = foreachTest1($testArr1);
        $for[$i] = forTest1($testArr1);
        $while[$i] = whileTest1($testArr1);
        $doWhile[$i] = doWhileTest1($testArr1);
    }
    printRes($for, $foreach, $while, $doWhile);
}
            $sum += $info['time'];
        }
        $data .= $cycle[$step][1]['iteration'] . ':' . $sum / sizeof($res) . "\n";
    }
    $path = 'test--' . $cycle[$maxStep][1]['cycle'] . ".txt";
    var_dump($path);
    file_put_contents($path, $data);
}
?>

<h1> Тест скорости циклов </h1>

<?php 
$from = 0;
$accuracy = 20;
$maxStep = 7;
for ($step = 4; $step <= $maxStep; $step++) {
    $to = pow(10, $step);
    for ($iter = 0; $iter <= $accuracy; $iter++) {
        $cycle[$step][$iter] = forTest1($from, $to);
    }
    unset($cycle[$step][0]);
    //$cycle[$step]['averageTime'] = array_sum($cycle[$step]) / sizeof($cycle[$step]);
}
echo '<h2>Умножение чисел</h2>';
echo "Тест - " . $cycle[$maxStep][1]['cycle'] . " для " . $cycle[$maxStep][1]['iteration'] . " повторений<br>";
echo '<pre>';
print_r($cycle);
echo '</pre>';
printInfile($cycle, $maxStep);
echo '<h2>Каждый елемент </h2>';