3v4l.org

run code in 500+ 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 substrReplaceCombine($array) { return array_combine( substr_replace($array, 'key', 0, 0), substr_replace($array, 'value', 0, 0) ); } 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', 'substrReplaceCombine', '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/ri89S
function name:  (null)
number of ops:  22
compiled vars:  !0 = $array, !1 = $test
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   96     0  E >   INIT_FCALL                                                   'range'
          1        SEND_VAL                                                     0
          2        SEND_VAL                                                     5000
          3        DO_ICALL                                             $2      
          4        ASSIGN                                                       !0, $2
   98     5      > FE_RESET_R                                           $4      <array>, ->20
          6    > > FE_FETCH_R                                                   $4, !1, ->20
   99     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
   98    19      > JMP                                                          ->6
         20    >   FE_FREE                                                      $4
  100    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/ri89S
function name:  {closure:/in/ri89S:99}
number of ops:  7
compiled vars:  !0 = $test, !1 = $array
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   99     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/ri89S
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/ri89S
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/ri89S
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 substrreplacecombine:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ri89S
function name:  substrReplaceCombine
number of ops:  19
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                                                   'substr_replace'
          3        SEND_VAR                                                     !0
          4        SEND_VAL                                                     'key'
          5        SEND_VAL                                                     0
          6        SEND_VAL                                                     0
          7        DO_ICALL                                             $1      
          8        SEND_VAR                                                     $1
   24     9        INIT_FCALL                                                   'substr_replace'
         10        SEND_VAR                                                     !0
         11        SEND_VAL                                                     'value'
         12        SEND_VAL                                                     0
         13        SEND_VAL                                                     0
         14        DO_ICALL                                             $2      
         15        SEND_VAR                                                     $2
   22    16        DO_ICALL                                             $3      
   24    17      > RETURN                                                       $3
   26    18*     > RETURN                                                       null

End of function substrreplacecombine

Function mapcombine:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ri89S
function name:  mapCombine
number of ops:  17
compiled vars:  !0 = $array
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   28     0  E >   RECV                                                 !0      
   29     1        INIT_FCALL                                                   'array_combine'
   30     2        INIT_FCALL                                                   'array_map'
   31     3        DECLARE_LAMBDA_FUNCTION                              ~1      [0]
          4        SEND_VAL                                                     ~1
   32     5        SEND_VAR                                                     !0
   30     6        DO_ICALL                                             $2      
   32     7        SEND_VAR                                                     $2
   34     8        INIT_FCALL                                                   'array_map'
   35     9        DECLARE_LAMBDA_FUNCTION                              ~3      [1]
         10        SEND_VAL                                                     ~3
   36    11        SEND_VAR                                                     !0
   34    12        DO_ICALL                                             $4      
   36    13        SEND_VAR                                                     $4
   29    14        DO_ICALL                                             $5      
   36    15      > RETURN                                                       $5
   39    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/ri89S
function name:  {closure:mapCombine():31}
number of ops:  4
compiled vars:  !0 = $v
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   31     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/ri89S
function name:  {closure:mapCombine():35}
number of ops:  4
compiled vars:  !0 = $v
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   35     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/ri89S
function name:  mapFlatten
number of ops:  12
compiled vars:  !0 = $array
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   41     0  E >   RECV                                                 !0      
   42     1        INIT_FCALL                                                   'array_merge'
   43     2        INIT_FCALL                                                   'array_map'
   44     3        DECLARE_LAMBDA_FUNCTION                              ~1      [0]
          4        SEND_VAL                                                     ~1
   45     5        SEND_VAR                                                     !0
   43     6        DO_ICALL                                             $2      
   45     7        SEND_UNPACK                                                  $2
          8        CHECK_UNDEF_ARGS                                             
   42     9        DO_ICALL                                             $3      
   45    10      > RETURN                                                       $3
   48    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/ri89S
function name:  {closure:mapFlatten():44}
number of ops:  6
compiled vars:  !0 = $v
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   44     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/ri89S
function name:  mapUncolumn
number of ops:  13
compiled vars:  !0 = $array
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   50     0  E >   RECV                                                 !0      
   51     1        INIT_FCALL                                                   'array_column'
   52     2        INIT_FCALL                                                   'array_map'
   53     3        DECLARE_LAMBDA_FUNCTION                              ~1      [0]
          4        SEND_VAL                                                     ~1
   54     5        SEND_VAR                                                     !0
   52     6        DO_ICALL                                             $2      
   54     7        SEND_VAR                                                     $2
   56     8        SEND_VAL                                                     1
   57     9        SEND_VAL                                                     0
   51    10        DO_ICALL                                             $3      
   57    11      > RETURN                                                       $3
   59    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/ri89S
function name:  {closure:mapUncolumn():53}
number of ops:  7
compiled vars:  !0 = $v
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   53     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/ri89S
function name:  walk
number of ops:  10
compiled vars:  !0 = $array, !1 = $result
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   61     0  E >   RECV                                                 !0      
   62     1        ASSIGN                                                       !1, <array>
   63     2        INIT_FCALL                                                   'array_walk'
   64     3        SEND_REF                                                     !0
   65     4        DECLARE_LAMBDA_FUNCTION                              ~3      [0]
          5        BIND_LEXICAL                                                 ~3, !1
   67     6        SEND_VAL                                                     ~3
   63     7        DO_ICALL                                                     
   69     8      > RETURN                                                       !1
   70     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/ri89S
function name:  {closure:walk():65}
number of ops:  7
compiled vars:  !0 = $v, !1 = $result
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   65     0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
   66     2        CONCAT                                               ~2      'key', !0
          3        CONCAT                                               ~4      'value', !0
          4        ASSIGN_DIM                                                   !1, ~2
          5        OP_DATA                                                      ~4
   67     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/ri89S
function name:  reduce
number of ops:  11
compiled vars:  !0 = $array
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   72     0  E >   RECV                                                 !0      
   73     1        INIT_FCALL                                                   'array_reduce'
   74     2        SEND_VAR                                                     !0
   75     3        DECLARE_LAMBDA_FUNCTION                              ~1      [0]
   78     4        SEND_VAL                                                     ~1
   79     5        NEW                                                  $2      'ArrayObject'
          6        DO_FCALL                                          0          
          7        SEND_VAR                                                     $2
   73     8        DO_ICALL                                             $4      
   79     9      > RETURN                                                       $4
   81    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/ri89S
function name:  {closure:reduce():75}
number of ops:  8
compiled vars:  !0 = $result, !1 = $v
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   75     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   76     2        CONCAT                                               ~2      'key', !1
          3        CONCAT                                               ~4      'value', !1
          4        ASSIGN_DIM                                                   !0, ~2
          5        OP_DATA                                                      ~4
   77     6      > RETURN                                                       !0
   78     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/ri89S
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
-----------------------------------------------------------------------------------------
   83     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      20
   85     2        ASSIGN                                                       !2, <array>
   86     3        ASSIGN                                                       !3, 0
          4      > JMP                                                          ->19
   87     5    >   INIT_FCALL                                                   'microtime'
          6        SEND_VAL                                                     <true>
          7        DO_ICALL                                             $8      
          8        ASSIGN                                                       !4, $8
   88     9        INIT_DYNAMIC_CALL                                            !0
         10        DO_FCALL                                          0          
   89    11        INIT_FCALL                                                   'microtime'
         12        SEND_VAL                                                     <true>
         13        DO_ICALL                                             $11     
         14        ASSIGN                                                       !5, $11
   90    15        SUB                                                  ~14     !5, !4
         16        ASSIGN_DIM                                                   !2
         17        OP_DATA                                                      ~14
   86    18        PRE_INC                                                      !3
         19    >   IS_SMALLER                                                   !3, !1
         20      > JMPNZ                                                        ~16, ->5
   93    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
   94    27*     > RETURN                                                       null

End of function returntime

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
171.35 ms | 1400 KiB | 26 Q