Skip to content

Instantly share code, notes, and snippets.

@win0err
Last active September 3, 2017 11:23
Show Gist options
  • Save win0err/67e579feaee4b2ca1c142219b21ca111 to your computer and use it in GitHub Desktop.
Save win0err/67e579feaee4b2ca1c142219b21ca111 to your computer and use it in GitHub Desktop.
Тест различных циклов в PHP на время выполнения и потребление памяти
<?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;
}
@win0err
Copy link
Author

win0err commented Sep 2, 2017

Результаты

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment