3v4l.org

run code in 300+ PHP versions simultaneously
<?php function generator($array, $prefix) { foreach ($array as $k => $v) { yield $prefix . $k => $v; } } function generatorPrefix($array, $prefix = 'prefix') { return iterator_to_array(generator($array, $prefix)); } function constructPrefix($array, $prefix = 'prefix') { // 1 loop over data $result = []; foreach ($array as $k => $v) { $result[$prefix . $k] = $v; } return $result; } function mapKeyAndCombinePrefix($array, $prefix = 'prefix') { // 3 loops over data return array_combine( array_map( fn($k) => $prefix . $k, array_keys($array) ), $array ); } function walkPrefix($array, $prefix = 'prefix') { // 1 loop over data $result = []; array_walk( $array, function($v, $k, $prefix) use (&$result) { $result[$prefix . $k] = $v; }, $prefix ); return $result; } function reducePrefix($array, $prefix = 'prefix') { // 2 loops over data return array_reduce( array_keys($array), function($result, $k) use ($array, $prefix) { $result[$prefix . $k] = $array[$k]; return $result; }, [] ); } 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 (['generatorPrefix', 'constructPrefix', 'mapKeyAndCombinePrefix', 'walkPrefix', 'reducePrefix'] 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/j9Reg
function name:  (null)
number of ops:  22
compiled vars:  !0 = $array, !1 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   INIT_FCALL                                               'range'
          1        SEND_VAL                                                 0
          2        SEND_VAL                                                 5000
          3        DO_ICALL                                         $2      
          4        ASSIGN                                                   !0, $2
   69     5      > FE_RESET_R                                       $4      <array>, ->20
          6    > > FE_FETCH_R                                               $4, !1, ->20
   70     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                                  '%00%7Bclosure%7D%2Fin%2Fj9Reg%3A70%243'
         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
   69    19      > JMP                                                      ->6
         20    >   FE_FREE                                                  $4
   71    21      > RETURN                                                   1

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

End of function generator

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

End of function generatorprefix

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

End of function constructprefix

Function mapkeyandcombineprefix:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/j9Reg
function name:  mapKeyAndCombinePrefix
number of ops:  17
compiled vars:  !0 = $array, !1 = $prefix
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'prefix'
   22     2        INIT_FCALL                                               'array_combine'
   23     3        INIT_FCALL                                               'array_map'
   24     4        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fj9Reg%3A24%240'
          5        BIND_LEXICAL                                             ~2, !1
          6        SEND_VAL                                                 ~2
   25     7        INIT_FCALL                                               'array_keys'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $3      
         10        SEND_VAR                                                 $3
         11        DO_ICALL                                         $4      
         12        SEND_VAR                                                 $4
   27    13        SEND_VAR                                                 !0
         14        DO_ICALL                                         $5      
         15      > RETURN                                                   $5
   29    16*     > RETURN                                                   null

End of function mapkeyandcombineprefix

Function %00%7Bclosure%7D%2Fin%2Fj9Reg%3A24%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/j9Reg
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $k, !1 = $prefix
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        CONCAT                                           ~2      !1, !0
          3      > RETURN                                                   ~2
          4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fj9Reg%3A24%240

Function walkprefix:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/j9Reg
function name:  walkPrefix
number of ops:  12
compiled vars:  !0 = $array, !1 = $prefix, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'prefix'
   32     2        ASSIGN                                                   !2, <array>
   33     3        INIT_FCALL                                               'array_walk'
   34     4        SEND_REF                                                 !0
   35     5        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fj9Reg%3A35%241'
          6        BIND_LEXICAL                                             ~4, !2
   37     7        SEND_VAL                                                 ~4
   38     8        SEND_VAR                                                 !1
          9        DO_ICALL                                                 
   40    10      > RETURN                                                   !2
   41    11*     > RETURN                                                   null

End of function walkprefix

Function %00%7Bclosure%7D%2Fin%2Fj9Reg%3A35%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/j9Reg
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $v, !1 = $k, !2 = $prefix, !3 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        BIND_STATIC                                              !3
   36     4        CONCAT                                           ~4      !2, !1
          5        ASSIGN_DIM                                               !3, ~4
          6        OP_DATA                                                  !0
   37     7      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fj9Reg%3A35%241

Function reduceprefix:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/j9Reg
function name:  reducePrefix
number of ops:  15
compiled vars:  !0 = $array, !1 = $prefix
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      'prefix'
   44     2        INIT_FCALL                                               'array_reduce'
   45     3        INIT_FCALL                                               'array_keys'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $2      
          6        SEND_VAR                                                 $2
   46     7        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fj9Reg%3A46%242'
          8        BIND_LEXICAL                                             ~3, !0
          9        BIND_LEXICAL                                             ~3, !1
   49    10        SEND_VAL                                                 ~3
   50    11        SEND_VAL                                                 <array>
         12        DO_ICALL                                         $4      
         13      > RETURN                                                   $4
   52    14*     > RETURN                                                   null

End of function reduceprefix

Function %00%7Bclosure%7D%2Fin%2Fj9Reg%3A46%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/j9Reg
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $result, !1 = $k, !2 = $array, !3 = $prefix
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
   47     4        CONCAT                                           ~4      !3, !1
          5        FETCH_DIM_R                                      ~6      !2, !1
          6        ASSIGN_DIM                                               !0, ~4
          7        OP_DATA                                                  ~6
   48     8      > RETURN                                                   !0
   49     9*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fj9Reg%3A46%242

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/j9Reg
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
-------------------------------------------------------------------------------------
   54     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      20
   56     2        ASSIGN                                                   !2, <array>
   57     3        ASSIGN                                                   !3, 0
          4      > JMP                                                      ->19
   58     5    >   INIT_FCALL                                               'microtime'
          6        SEND_VAL                                                 <true>
          7        DO_ICALL                                         $8      
          8        ASSIGN                                                   !4, $8
   59     9        INIT_DYNAMIC_CALL                                        !0
         10        DO_FCALL                                      0          
   60    11        INIT_FCALL                                               'microtime'
         12        SEND_VAL                                                 <true>
         13        DO_ICALL                                         $11     
         14        ASSIGN                                                   !5, $11
   61    15        SUB                                              ~14     !5, !4
         16        ASSIGN_DIM                                               !2
         17        OP_DATA                                                  ~14
   57    18        PRE_INC                                                  !3
         19    >   IS_SMALLER                                               !3, !1
         20      > JMPNZ                                                    ~16, ->5
   64    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
   65    27*     > RETURN                                                   null

End of function returntime

Function %00%7Bclosure%7D%2Fin%2Fj9Reg%3A70%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/j9Reg
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $test, !1 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     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 function %00%7Bclosure%7D%2Fin%2Fj9Reg%3A70%243

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
138.03 ms | 1419 KiB | 33 Q