Last active
December 15, 2015 12:29
-
-
Save matsubo/5260716 to your computer and use it in GitHub Desktop.
Benchmark of "for loop" vs "foreach range loop".
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 -v | |
PHP 5.3.15 with Suhosin-Patch (cli) (built: Aug 24 2012 17:45:44) | |
% php loop.php | |
0.019730806350708 | |
0.14999198913574 | |
7.6 times faster than range. |
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 | |
$times = 500000; | |
$start = microtime(true); | |
for ($i=0;$i<$times;$i++) { | |
} | |
$for = microtime(true) - $start ; | |
print $for ."\n"; | |
$start = microtime(true); | |
foreach (range(0, $times) as $i) { | |
} | |
$range = microtime(true) - $start; | |
print $range ."\n"; | |
print round($range/$for,1)." times faster than range.\n"; |
Author
matsubo
commented
Mar 28, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment