Created
October 25, 2019 03:32
-
-
Save shtrom/12afa94c5b68cb19c2b7bff875f0e75b to your computer and use it in GitHub Desktop.
Comparing PHP array_* functions vs. foreach.
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
$referenceMap = [ "sessRef1" => "ibkRef1", "sessRef2" => "ibkRef2", "sessRef3" => null, "sessRef1_2" => "ibkRef1" ]; | |
function test_funcs(array $map) { $values = array_values($map); $newMap = array_merge($map, array_combine($values, $values)); return $newMap; } | |
function test_foreach(array $map) { $newMap=[]; foreach($map as $k => $v) { $newMap[$k] = $v; $newMap[$v] = $v; } return $newMap; } | |
function fmicrotime_funcs($map, $count=10000) { $time=microtime(true); for($i=0;$i<$count;$i++) { test_funcs($map); }; $delta=microtime(true)-$time; echo $delta; }; | |
function fmicrotime_foreach($map, $count=10000) { $time=microtime(true); for($i=0;$i<$count;$i++) { test_foreach($map); }; $delta=microtime(true)-$time; echo $delta; } |
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
> fmicrotime_funcs($referenceMap, 10000000); | |
3.0302679538727 | |
php > fmicrotime_foreach($referenceMap, 10000000); | |
2.7838201522827 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment