Skip to content

Instantly share code, notes, and snippets.

@chrisciampoli
Created March 8, 2016 00:22
Show Gist options
  • Save chrisciampoli/ba351b4e95084f9f1404 to your computer and use it in GitHub Desktop.
Save chrisciampoli/ba351b4e95084f9f1404 to your computer and use it in GitHub Desktop.
array_map vs foreach testing PHP
1
down vote
It's interesting. But I've got an opposite result with the following codes which are simplified from my current projects:
// test a simple array_map in the real world.
function test_array_map($data){
return array_map(function($row){
return array(
'productId' => $row['id'] + 1,
'productName' => $row['name'],
'desc' => $row['remark']
);
}, $data);
}
// Another with local variable $i
function test_array_map_use_local($data){
$i = 0;
return array_map(function($row) use ($i) {
$i++;
return array(
'productId' => $row['id'] + $i,
'productName' => $row['name'],
'desc' => $row['remark']
);
}, $data);
}
// test a simple foreach in the real world
function test_foreach($data){
$result = array();
foreach ($data as $row) {
$tmp = array();
$tmp['productId'] = $row['id'] + 1;
$tmp['productName'] = $row['name'];
$tmp['desc'] = $row['remark'];
$result[] = $tmp;
}
return $result;
}
// Another with local variable $i
function test_foreach_use_local($data){
$result = array();
$i = 0;
foreach ($data as $row) {
$i++;
$tmp = array();
$tmp['productId'] = $row['id'] + $i;
$tmp['productName'] = $row['name'];
$tmp['desc'] = $row['remark'];
$result[] = $tmp;
}
return $result;
}
Here is my testing data and codes:
$data = array_fill(0, 10000, array(
'id' => 1,
'name' => 'test',
'remark' => 'ok'
));
$tests = array(
'array_map' => array(),
'foreach' => array(),
'array_map_use_local' => array(),
'foreach_use_local' => array(),
);
for ($i = 0; $i < 100; $i++){
foreach ($tests as $testName => &$records) {
$start = microtime(true);
call_user_func("test_$testName", $data);
$delta = microtime(true) - $start;
$records[] = $delta;
}
}
// output result:
foreach ($tests as $name => &$records) {
printf('%.4f : %s '.PHP_EOL,
array_sum($records) / count($records), $name);
}
The result is:
0.0098 : array_map
0.0114 : foreach
0.0114 : array_map_use_local
0.0115 : foreach_use_local
My tests were in LAMP production environment without xdebug. I'am wandering xdebug would slow down array_map's performance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment