Skip to content

Instantly share code, notes, and snippets.

@shtrom
Created October 25, 2019 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shtrom/12afa94c5b68cb19c2b7bff875f0e75b to your computer and use it in GitHub Desktop.
Save shtrom/12afa94c5b68cb19c2b7bff875f0e75b to your computer and use it in GitHub Desktop.
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