Last active
September 3, 2017 11:23
-
-
Save win0err/67e579feaee4b2ca1c142219b21ca111 to your computer and use it in GitHub Desktop.
Тест различных циклов в PHP на время выполнения и потребление памяти
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo 'PHP ' . phpversion() . PHP_EOL . PHP_EOL; | |
$test = []; | |
for($i = 0; $i < 100000; $i++) | |
$test[$i] = $i; | |
process_test(function() use ($test) { | |
foreach($test as $t) | |
continue; | |
}); | |
process_test(function() use ($test) { | |
for($a = 0; $a < sizeof($test); $a++) | |
continue; | |
}); | |
process_test(function() use ($test) { | |
for($a = 0, $b = sizeof($test); $a < $b; $a++) | |
continue; | |
}); | |
function process_test($callback) { | |
static $runNumber = 0; | |
$runNumber++; | |
$startedAt = microtime(true); | |
$callback(); | |
echo 'Test #' . $runNumber . ': ' . ( microtime(true) - $startedAt ) . ' seconds'. PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Результаты
PHP 7.0.14
Test #1: 0.0010159015655518 seconds
Test #2: 0.0030970573425293 seconds
Test #3: 0.0013790130615234 seconds
PHP 5.6.29
Test #1: 0.018749952316284 seconds
Test #2: 0.011775016784668 seconds
Test #3: 0.0035569667816162 seconds