Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Comparing PHP array_* functions vs. foreach.
$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; }
> 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
You can’t perform that action at this time.