3 回答
TA贡献1854条经验 获得超8个赞
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";}
在十几次尝试中,我获得了非常一致的结果,包括1M数字:
Foreach:0.7秒
关闭地图:3.4秒
功能名称上的映射:1.2秒。
假设关闭时地图的速度低迷是因为关闭可能每次都被评估,我也测试过这样:
function useMapClosure($numbers) { $closure = function($number) { return $number * 10; }; return array_map($closure, $numbers);}
但结果是相同的,证实封闭只评估一次。
以下是三个回调的操作码转储。第一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
TA贡献1846条经验 获得超7个赞
这真有趣。但是我从以下代码得到了相反的结果,这些代码从我当前的项目中简化了:
// 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 $ifunction 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 worldfunction 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 $ifunction 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:array_map0.0114:foreach0.0114:array_map_use_local0.0115:foreach_use_local
我的测试是在没有xdebug的LAMP生产环境中进行的。我徘徊xdebug会降低array_map的性能。
- 3 回答
- 0 关注
- 634 浏览
添加回答
举报