foreach、带有 lambda 的 array_map 和带有静态函数的 array_map 的性能 [英] Performance of foreach, array_map with lambda and array_map with static function

查看:26
本文介绍了foreach、带有 lambda 的 array_map 和带有静态函数的 array_map 的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这三种用于将数组转换为另一个数组的方法之间的性能差异(如果有的话)是什么?

What's the performance difference (if there is any) between these three approaches, both used to transform an array to another array?

  1. 使用foreach
  2. array_map 与 lambda/闭包函数一起使用
  3. array_map 与静态"函数/方法一起使用
  4. 还有其他方法吗?
  1. Using foreach
  2. Using array_map with lambda/closure function
  3. Using array_map with 'static' function/method
  4. Is there any other approach?

为了清楚起见,让我们看一下示例,所有示例都执行相同的操作 - 将数字数组乘以 10:

To make myself clear, let's have look at the examples, all doing the same - multiplying the array of numbers by 10:

$numbers = range(0, 1000);

<小时>

Foreach

$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);

<小时>

还有其他方法吗?我会很高兴听到上述案例之间的所有差异,以及为什么应该使用一个而不是其他的任何输入.


Is there any other approach? I will be happy to hear actually all differences between the cases from above, and any inputs why one should be used instead of others.

推荐答案

FWIW,我只是做了基准测试,因为海报没有做.在 PHP 5.3.10 + XDebug 上运行.

FWIW, I just did the benchmark since poster didn't do it. Running on PHP 5.3.10 + XDebug.

更新 2015-01-22 与下面 mcfedr 的回答进行比较,以获得没有 XDebug 和更新的 PHP 版本的额外结果.

UPDATE 2015-01-22 compare with mcfedr's answer below for additional results without XDebug and a more recent PHP version.


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
";
}

我通过十几次尝试得到了 100 万个数字的非常一致的结果:

I get pretty consistent results with 1M numbers across a dozen attempts:

  • Foreach:0.7 秒
  • 关闭地图:3.4 秒
  • 映射函数名称:1.2 秒.

假设关闭时地图速度慢是由于可能每次都在评估关闭造成的,我也这样测试:

Supposing the lackluster speed of the map on closure was caused by the closure possibly being evaluated each time, I also tested like this:


function useMapClosure($numbers) {
  $closure = function($number) {
    return $number * 10;
  };

  return array_map($closure, $numbers);
}

但结果是相同的,确认闭包只计算一次.

But the results are identical, confirming that the closure is only evaluated once.

2014-02-02 更新:操作码转储

2014-02-02 UPDATE: opcodes dump

这是三个回调的操作码转储.首先useForeach():

Here are the opcode dumps for the three callbacks. First 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():

and the named function it calls, _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

这篇关于foreach、带有 lambda 的 array_map 和带有静态函数的 array_map 的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆