Last active
September 26, 2018 19:19
-
-
Save jonathanlaf/f973d9cb5448e362a2bd4217c1d46873 to your computer and use it in GitHub Desktop.
[Benchmark Loops] Benchmark of FOR vs FOREACH loop functions in php #php #benchmark
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 | |
$a = array(); | |
for ($i = 0; $i < 100000; $i++) { | |
$a[] = $i; | |
} | |
$start = microtime(true); | |
foreach ($a as $k => $v) { | |
$a[$k] = $v + 1; | |
} | |
echo "1- Completed in ", microtime(true) - $start, " Seconds\n"; | |
$start = microtime(true); | |
foreach ($a as $k => &$v) { | |
$v = $v + 1; | |
} | |
echo "2- Completed in ", microtime(true) - $start, " Seconds\n"; | |
$start = microtime(true); | |
foreach ($a as $k => $v) {} | |
echo "3- Completed in ", microtime(true) - $start, " Seconds\n"; | |
$start = microtime(true); | |
foreach ($a as $k => &$v) {} | |
echo "4- Completed in ", microtime(true) - $start, " Seconds\n"; | |
$start = microtime(true); | |
$countFor = count($a); | |
for ($i=0;$i<$countFor;$i++) { | |
$a[$i] = $a[$i]+1; | |
} | |
echo "5- Completed in ", microtime(true) - $start, " Seconds\n"; | |
$start = microtime(true); | |
$countFor = count($a); | |
for ($i=0;$i<=$countFor;$i++) {} | |
echo "6- Completed in ", microtime(true) - $start, " Seconds\n"; |
DarckBlezzer
commented
Sep 26, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment