这三种方法都用于将一个数组转换为另一个数组,其性能差异是什么(如果有)?
foreach
array_map
与 lambda/闭包函数一起使用array_map
与“静态”函数/方法一起使用为了让自己更清楚,让我们看一下示例,所有示例都执行相同的操作 - 将数字数组乘以 10:
$numbers = range(0, 1000);
对于每个
$result = array();
foreach ($numbers as $number) {
$result[] = $number * 10;
}
return $result;
使用 lambda 进行映射
return array_map(function($number) {
return $number * 10;
}, $numbers);
具有“静态”函数的映射,作为字符串引用传递
function tenTimes($number) {
return $number * 10;
}
return array_map('tenTimes', $numbers);
还有其他方法吗?我很高兴听到上述案例之间的实际所有差异,以及为什么应该使用其中一个而不是其他的任何输入。
在禁用 xdebug 的情况下运行此基准测试很有趣,因为 xdebug 增加了相当多的开销,尤其是函数调用。
这是使用5.6运行的FGM脚本 使用 xdebug
ForEach : 0.79232501983643
MapClosure: 4.1082420349121
MapNamed : 1.7884571552277
没有 xdebug
ForEach : 0.69830799102783
MapClosure: 0.78584599494934
MapNamed : 0.85125398635864
这里foreach 和closure 版本之间只有很小的区别。
添加带有
use
的闭合版本也很有趣
function useMapClosureI($numbers) {
$i = 10;
return array_map(function($number) use ($i) {
return $number * $i++;
}, $numbers);
}
为了比较我添加:
function useForEachI($numbers) {
$result = array();
$i = 10;
foreach ($numbers as $number) {
$result[] = $number * $i++;
}
return $result;
}
在这里我们可以看到它对闭包版本产生了影响,而数组没有明显改变。
19/11/2015 我现在还添加了使用 PHP 7 和 HHVM 的结果进行比较。结论是相似的,尽管一切都快得多。
PHP 5.6
ForEach : 0.57499806880951
MapClosure : 0.59327731132507
MapNamed : 0.69694859981537
MapClosureI: 0.73265469074249
ForEachI : 0.60068697929382
PHP 7
ForEach : 0.11297199726105
MapClosure : 0.16404168605804
MapNamed : 0.11067249774933
MapClosureI: 0.19481580257416
ForEachI : 0.10989861488342
HHVM
ForEach : 0.090071058273315
MapClosure : 0.10432276725769
MapNamed : 0.1091267824173
MapClosureI: 0.11197068691254
ForEachI : 0.092114186286926
FWIW,我只是做了基准测试,因为海报没有这样做。在 PHP 5.3.10 + XDebug 上运行。
更新 2015-01-22 与下面 mcfedr 的答案进行比较,以获取没有 XDebug 和更新的 PHP 版本的其他结果。
function lap($func) {
$t0 = microtime(1);
$numbers = range(0, 1000000);
$ret = $func($numbers);
$t1 = microtime(1);
return array($t1 - $t0, $ret);
}
function useForeach($numbers) {
$result = array();
foreach ($numbers as $number) {
$result[] = $number * 10;
}
return $result;
}
function useMapClosure($numbers) {
return array_map(function($number) {
return $number * 10;
}, $numbers);
}
function _tenTimes($number) {
return $number * 10;
}
function useMapNamed($numbers) {
return array_map('_tenTimes', $numbers);
}
foreach (array('Foreach', 'MapClosure', 'MapNamed') as $callback) {
list($delay,) = lap("use$callback");
echo "$callback: $delay\n";
}
我在十几次尝试中得到了 100 万个数字的非常一致的结果:
假设关闭时地图速度不佳是因为可能每次都会评估关闭造成的,我也这样测试:
function useMapClosure($numbers) {
$closure = function($number) {
return $number * 10;
};
return array_map($closure, $numbers);
}
但结果是相同的,确认闭包仅被评估一次。
2014-02-02 更新:操作码转储
以下是三个回调的操作码转储。首先
useForeach()
:
compiled vars: !0 = $numbers, !1 = $result, !2 = $number
line # * op fetch ext return operands
---------------------------------------------------------------------------------
10 0 > EXT_NOP
1 RECV 1
11 2 EXT_STMT
3 INIT_ARRAY ~0
4 ASSIGN !1, ~0
12 5 EXT_STMT
6 > FE_RESET $2 !0, ->15
7 > > FE_FETCH $3 $2, ->15
8 > OP_DATA
9 ASSIGN !2, $3
13 10 EXT_STMT
11 MUL ~6 !2, 10
12 ASSIGN_DIM !1
13 OP_DATA ~6, $7
14 14 > JMP ->7
15 > SWITCH_FREE $2
15 16 EXT_STMT
17 > RETURN !1
16 18* EXT_STMT
19* > RETURN null
然后
useMapClosure()
compiled vars: !0 = $numbers
line # * op fetch ext return operands
---------------------------------------------------------------------------------
18 0 > EXT_NOP
1 RECV 1
19 2 EXT_STMT
3 EXT_FCALL_BEGIN
4 DECLARE_LAMBDA_FUNCTION '%00%7Bclosure%7D%2Ftmp%2Flap.php0x7f7fc1424173'
21 5 SEND_VAL ~0
6 SEND_VAR !0
7 DO_FCALL 2 $1 'array_map'
8 EXT_FCALL_END
9 > RETURN $1
22 10* EXT_STMT
11* > RETURN null
以及它调用的闭包:
compiled vars: !0 = $number
line # * op fetch ext return operands
---------------------------------------------------------------------------------
19 0 > EXT_NOP
1 RECV 1
20 2 EXT_STMT
3 MUL ~0 !0, 10
4 > RETURN ~0
21 5* EXT_STMT
6* > RETURN null
然后是
useMapNamed()
功能:
compiled vars: !0 = $numbers
line # * op fetch ext return operands
---------------------------------------------------------------------------------
28 0 > EXT_NOP
1 RECV 1
29 2 EXT_STMT
3 EXT_FCALL_BEGIN
4 SEND_VAL '_tenTimes'
5 SEND_VAR !0
6 DO_FCALL 2 $0 'array_map'
7 EXT_FCALL_END
8 > RETURN $0
30 9* EXT_STMT
10* > RETURN null
以及它调用的命名函数,
_tenTimes()
:
compiled vars: !0 = $number
line # * op fetch ext return operands
---------------------------------------------------------------------------------
24 0 > EXT_NOP
1 RECV 1
25 2 EXT_STMT
3 MUL ~0 !0, 10
4 > RETURN ~0
26 5* EXT_STMT
6* > RETURN null
以下是当前 PHP 8 (RC2) 版本的一些更新测试。 还添加了短闭包
PHP 8.0 RC2
Foreach: 0.093745978673299
MapClosure: 0.096948345502218
MapShortClosure: 0.096264243125916
MapNamed: 0.091399153073629
MapClosureI: 0.11352666219076
ForEachI: 0.097501540184021
这很有趣。但我得到了相反的结果,以下代码是从我当前的项目中简化的:
// 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;
}
这是我的测试数据和代码:
$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);
}
结果是:
0.0098:数组映射 0.0114:foreach 0.0114:数组映射使用本地 0.0115:foreach_use_local
我的测试是在没有xdebug的LAMP生产环境中进行的。 我在徘徊 xdebug 会降低 array_map 的性能。
我尝试在 PHP 8 和 Windows 10 上测试 @FGM 的代码 10 次。这是结果: 图片
我不知道PHP是否可以有JIT。我猜它在 PHP8 中有 JIT,因为在文件 php.ini 中,我在 php.ini 中看到了 1 个配置命令:auto_globals_jit=On。
$ php -v
PHP 8.3.11 (cli) (built: Aug 27 2024 19:16:34) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.3.11, Copyright (c) Zend Technologies
with the ionCube PHP Loader v13.3.1, Copyright (c) 2002-2024, by ionCube Ltd.
with Zend OPcache v8.3.11, Copyright (c), by Zend Technologies
array_map execution time: 0.040987968444824 seconds
foreach execution time: 0.051311016082764 seconds
array_map execution time: 0.04297399520874 seconds
foreach execution time: 0.052814960479736 seconds
Both methods produce the same result.
array (
0 => 2,
1 => 4,
2 => 6,
3 => 8,
4 => 10,
5 => 12,
6 => 14,
7 => 16,
8 => 18,
9 => 20,
// Sample data array
$array = range(1, 1000000); // Array with 1M elements
// Operation: A simple operation that will be applied to each element
function operation($n)
{
return $n * 2;
}
// Measure execution time for array_map
$startTime = microtime(true);
$result_map = array_map('operation', $array);
$endTime = microtime(true);
$map_time = $endTime - $startTime;
echo 'array_map execution time: '.$map_time." seconds\n";
// Measure execution time for foreach loop
$startTime = microtime(true);
$result_foreach = [];
foreach ($array as $n) {
$result_foreach[] = operation($n);
}
$endTime = microtime(true);
$foreach_time = $endTime - $startTime;
echo 'foreach execution time: '.$foreach_time." seconds\n";
// Measure execution time for array_map
$startTime = microtime(true);
$result_map = array_map('operation', $array);
$endTime = microtime(true);
$map_time = $endTime - $startTime;
echo 'array_map execution time: '.$map_time." seconds\n";
// Measure execution time for foreach loop
$startTime = microtime(true);
$result_foreach = [];
foreach ($array as $n) {
$result_foreach[] = operation($n);
}
$endTime = microtime(true);
$foreach_time = $endTime - $startTime;
echo 'foreach execution time: '.$foreach_time." seconds\n";
// Compare results
if ($result_map === $result_foreach) {
echo "Both methods produce the same result.\n";
} else {
echo "Results differ between array_map and foreach.\n";
}
var_export(array_slice($result_map, 0, 10));