3v4l.org

run code in 300+ PHP versions simultaneously
<?php function generate($array) { // 1 loop over data foreach ($array as $v) { yield 'key' . $v => 'value' . $v; } } function generator($array) { // 1 parent loop over data return iterator_to_array(generate($array)); } function construct($array) { // 1 loop over data $result = []; foreach ($array as $v) { $result['key' . $v] = 'value' . $v; } return $result; } function mapCombine($array) { // 3 loops over data return array_combine( array_map( fn($v) => 'key' . $v, $array ), array_map( fn($v) => 'value' . $v, $array ), ); } function mapFlatten($array) { // 3 loops over data return array_merge( ...array_map( fn($v) => ['key' . $v => 'value' . $v], $array ), ); } function mapUncolumn($array) { // 2 loops over data return array_column( array_map( fn($v) => ['key' . $v, 'value' . $v], $array ), 1, 0 ); } function walk($array) { // 1 loop over data $result = []; array_walk( $array, function($v) use (&$result) { $result['key' . $v] = 'value' . $v; } ); return $result; } function reduce($array) { // 1 loop over data return array_reduce( $array, function($result, $v) { $result['key' . $v] = 'value' . $v; return $result; }, new ArrayObject() ); } function returnTime(callable $function, int $repeat = 20) { $tests = []; for ($i = 0; $i < $repeat; ++$i) { $startTime = microtime(true); $function(); $endTime = microtime(true); $tests[] = $endTime - $startTime; } // Representing the average return 1000 * array_sum($tests) / $repeat; } $array = range(0, 5000); foreach (['generator', 'construct', 'mapCombine', 'mapFlatten', 'mapUncolumn', 'walk', 'reduce'] as $test) { echo "Duration of $test: ", returnTime(fn() => $test($array)) . PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 20
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 20
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/57SrG
function name:  (null)
number of ops:  22
compiled vars:  !0 = $array, !1 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   89     0  E >   INIT_FCALL                                               'range'
          1        SEND_VAL                                                 0
          2        SEND_VAL                                                 5000
          3        DO_ICALL                                         $2      
          4        ASSIGN                                                   !0, $2
   91     5      > FE_RESET_R                                       $4      <array>, ->20
          6    > > FE_FETCH_R                                               $4, !1, ->20
   92     7    >   ROPE_INIT                                     3  ~6      'Duration+of+'
          8        ROPE_ADD                                      1  ~6      ~6, !1
          9        ROPE_END                                      2  ~5      ~6, '%3A+'
         10        ECHO                                                     ~5
         11        INIT_FCALL                                               'returntime'
         12        DECLARE_LAMBDA_FUNCTION                          ~8      [0]
         13        BIND_LEXICAL                                             ~8, !1
         14        BIND_LEXICAL                                             ~8, !0
         15        SEND_VAL                                                 ~8
         16        DO_FCALL                                      0  $9      
         17        CONCAT                                           ~10     $9, '%0A'
         18        ECHO                                                     ~10
   91    19      > JMP                                                      ->6
         20    >   FE_FREE                                                  $4
   93    21      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/57SrG
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $test, !1 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   92     0  E >   BIND_STATIC                                              !0
          1        BIND_STATIC                                              !1
          2        INIT_DYNAMIC_CALL                                        !0
          3        SEND_VAR_EX                                              !1
          4        DO_FCALL                                      0  $2      
          5      > RETURN                                                   $2
          6*     > RETURN                                                   null

End of Dynamic Function 0

Function generate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 8
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 8
filename:       /in/57SrG
function name:  generate
number of ops:  10
compiled vars:  !0 = $array, !1 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        GENERATOR_CREATE                                         
    4     2      > FE_RESET_R                                       $2      !0, ->8
          3    > > FE_FETCH_R                                               $2, !1, ->8
    5     4    >   CONCAT                                           ~3      'key', !1
          5        CONCAT                                           ~4      'value', !1
          6        YIELD                                                    ~4, ~3
    4     7      > JMP                                                      ->3
          8    >   FE_FREE                                                  $2
    7     9      > GENERATOR_RETURN                                         

End of function generate

Function generator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/57SrG
function name:  generator
number of ops:  9
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
   10     1        INIT_FCALL                                               'iterator_to_array'
          2        INIT_FCALL                                               'generate'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $1      
          5        SEND_VAR                                                 $1
          6        DO_ICALL                                         $2      
          7      > RETURN                                                   $2
   11     8*     > RETURN                                                   null

End of function generator

Function construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 9
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/57SrG
function name:  construct
number of ops:  12
compiled vars:  !0 = $array, !1 = $result, !2 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   14     1        ASSIGN                                                   !1, <array>
   15     2      > FE_RESET_R                                       $4      !0, ->9
          3    > > FE_FETCH_R                                               $4, !2, ->9
   16     4    >   CONCAT                                           ~5      'key', !2
          5        CONCAT                                           ~7      'value', !2
          6        ASSIGN_DIM                                               !1, ~5
          7        OP_DATA                                                  ~7
   15     8      > JMP                                                      ->3
          9    >   FE_FREE                                                  $4
   18    10      > RETURN                                                   !1
   19    11*     > RETURN                                                   null

End of function construct

Function mapcombine:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/57SrG
function name:  mapCombine
number of ops:  17
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   22     1        INIT_FCALL                                               'array_combine'
   23     2        INIT_FCALL                                               'array_map'
   24     3        DECLARE_LAMBDA_FUNCTION                          ~1      [0]
          4        SEND_VAL                                                 ~1
   25     5        SEND_VAR                                                 !0
   23     6        DO_ICALL                                         $2      
   25     7        SEND_VAR                                                 $2
   27     8        INIT_FCALL                                               'array_map'
   28     9        DECLARE_LAMBDA_FUNCTION                          ~3      [1]
         10        SEND_VAL                                                 ~3
   29    11        SEND_VAR                                                 !0
   27    12        DO_ICALL                                         $4      
   29    13        SEND_VAR                                                 $4
   22    14        DO_ICALL                                         $5      
   29    15      > RETURN                                                   $5
   32    16*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/57SrG
function name:  {closure}
number of ops:  4
compiled vars:  !0 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
          1        CONCAT                                           ~1      'key', !0
          2      > RETURN                                                   ~1
          3*     > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/57SrG
function name:  {closure}
number of ops:  4
compiled vars:  !0 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
          1        CONCAT                                           ~1      'value', !0
          2      > RETURN                                                   ~1
          3*     > RETURN                                                   null

End of Dynamic Function 1

End of function mapcombine

Function mapflatten:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/57SrG
function name:  mapFlatten
number of ops:  12
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
   35     1        INIT_FCALL                                               'array_merge'
   36     2        INIT_FCALL                                               'array_map'
   37     3        DECLARE_LAMBDA_FUNCTION                          ~1      [0]
          4        SEND_VAL                                                 ~1
   38     5        SEND_VAR                                                 !0
   36     6        DO_ICALL                                         $2      
   38     7        SEND_UNPACK                                              $2
          8        CHECK_UNDEF_ARGS                                         
   35     9        DO_ICALL                                         $3      
   38    10      > RETURN                                                   $3
   41    11*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/57SrG
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
          1        CONCAT                                           ~1      'key', !0
          2        CONCAT                                           ~2      'value', !0
          3        INIT_ARRAY                                       ~3      ~2, ~1
          4      > RETURN                                                   ~3
          5*     > RETURN                                                   null

End of Dynamic Function 0

End of function mapflatten

Function mapuncolumn:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/57SrG
function name:  mapUncolumn
number of ops:  13
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
   44     1        INIT_FCALL                                               'array_column'
   45     2        INIT_FCALL                                               'array_map'
   46     3        DECLARE_LAMBDA_FUNCTION                          ~1      [0]
          4        SEND_VAL                                                 ~1
   47     5        SEND_VAR                                                 !0
   45     6        DO_ICALL                                         $2      
   47     7        SEND_VAR                                                 $2
   49     8        SEND_VAL                                                 1
   50     9        SEND_VAL                                                 0
   44    10        DO_ICALL                                         $3      
   50    11      > RETURN                                                   $3
   52    12*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/57SrG
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
          1        CONCAT                                           ~1      'key', !0
          2        INIT_ARRAY                                       ~2      ~1
          3        CONCAT                                           ~3      'value', !0
          4        ADD_ARRAY_ELEMENT                                ~2      ~3
          5      > RETURN                                                   ~2
          6*     > RETURN                                                   null

End of Dynamic Function 0

End of function mapuncolumn

Function walk:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/57SrG
function name:  walk
number of ops:  10
compiled vars:  !0 = $array, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   RECV                                             !0      
   55     1        ASSIGN                                                   !1, <array>
   56     2        INIT_FCALL                                               'array_walk'
   57     3        SEND_REF                                                 !0
   58     4        DECLARE_LAMBDA_FUNCTION                          ~3      [0]
          5        BIND_LEXICAL                                             ~3, !1
   60     6        SEND_VAL                                                 ~3
   56     7        DO_ICALL                                                 
   62     8      > RETURN                                                   !1
   63     9*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/57SrG
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $v, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   59     2        CONCAT                                           ~2      'key', !0
          3        CONCAT                                           ~4      'value', !0
          4        ASSIGN_DIM                                               !1, ~2
          5        OP_DATA                                                  ~4
   60     6      > RETURN                                                   null

End of Dynamic Function 0

End of function walk

Function reduce:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/57SrG
function name:  reduce
number of ops:  11
compiled vars:  !0 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   RECV                                             !0      
   66     1        INIT_FCALL                                               'array_reduce'
   67     2        SEND_VAR                                                 !0
   68     3        DECLARE_LAMBDA_FUNCTION                          ~1      [0]
   71     4        SEND_VAL                                                 ~1
   72     5        NEW                                              $2      'ArrayObject'
          6        DO_FCALL                                      0          
          7        SEND_VAR                                                 $2
   66     8        DO_ICALL                                         $4      
   72     9      > RETURN                                                   $4
   74    10*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/57SrG
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $result, !1 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   68     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   69     2        CONCAT                                           ~2      'key', !1
          3        CONCAT                                           ~4      'value', !1
          4        ASSIGN_DIM                                               !0, ~2
          5        OP_DATA                                                  ~4
   70     6      > RETURN                                                   !0
   71     7*     > RETURN                                                   null

End of Dynamic Function 0

End of function reduce

Function returntime:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 5
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 5
Branch analysis from position: 21
Branch analysis from position: 5
filename:       /in/57SrG
function name:  returnTime
number of ops:  28
compiled vars:  !0 = $function, !1 = $repeat, !2 = $tests, !3 = $i, !4 = $startTime, !5 = $endTime
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   76     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      20
   78     2        ASSIGN                                                   !2, <array>
   79     3        ASSIGN                                                   !3, 0
          4      > JMP                                                      ->19
   80     5    >   INIT_FCALL                                               'microtime'
          6        SEND_VAL                                                 <true>
          7        DO_ICALL                                         $8      
          8        ASSIGN                                                   !4, $8
   81     9        INIT_DYNAMIC_CALL                                        !0
         10        DO_FCALL                                      0          
   82    11        INIT_FCALL                                               'microtime'
         12        SEND_VAL                                                 <true>
         13        DO_ICALL                                         $11     
         14        ASSIGN                                                   !5, $11
   83    15        SUB                                              ~14     !5, !4
         16        ASSIGN_DIM                                               !2
         17        OP_DATA                                                  ~14
   79    18        PRE_INC                                                  !3
         19    >   IS_SMALLER                                               !3, !1
         20      > JMPNZ                                                    ~16, ->5
   86    21    >   INIT_FCALL                                               'array_sum'
         22        SEND_VAR                                                 !2
         23        DO_ICALL                                         $17     
         24        MUL                                              ~18     $17, 1000
         25        DIV                                              ~19     ~18, !1
         26      > RETURN                                                   ~19
   87    27*     > RETURN                                                   null

End of function returntime

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.44 ms | 1033 KiB | 25 Q