3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = [ 'a', ['b', 'c'], 'd', ['e'], ['f', 'g'], ['h', 'i', ['j', ['k'], ['l', 'm', ['n']]]], 'o', 'p', ['q', 'r'], ['s', ['t', ['u', ['v']]]], 'w', 'x', 'y', 'z']; class TraversableInput implements \IteratorAggregate { public $input = [ 'a', ['b', 'c'], 'd', ['e'], ['f', 'g'], ['h', 'i', ['j', ['k'], ['l', 'm', ['n']]]], 'o', 'p', ['q', 'r'], ['s', ['t', ['u', ['v']]]], 'w', 'x', 'y', 'z']; function getIterator() { return new ArrayIterator($this->input); } } $input = new TraversableInput(); function flatten1($input) { return \array_reduce( $input, function ($carry, $item) { return \is_array($item) ? \array_merge($carry, flatten1($item)) : \array_merge($carry, [$item]); }, [] ); } function flatten2($input) { $stack = [$input]; $result = []; while (!empty($stack)) { $item = array_shift($stack); if (is_array($item) || $item instanceof Traversable) { foreach ($item as $element) { array_unshift($stack, $element); } } else { array_unshift($result, $item); } } return $result; } $start = microtime(TRUE); for($x=0;$x<10000;$x++){ flatten1($input); }; $end = microtime(TRUE); $diff = $end-$start; $sec = intval($diff); $micro = $diff - $sec; $final = strftime('%T', mktime(0, 0, $sec)) . str_replace('0.', '.', sprintf('%.10f', $micro)); echo "Flatten1: " . $final . "\n"; $start = microtime(TRUE); for($x=0;$x<10000;$x++){ flatten2($input); }; $end = microtime(TRUE); $diff = $end-$start; $sec = intval($diff); $micro = $diff - $sec; $final = strftime('%T', mktime(0, 0, $sec)) . str_replace('0.', '.', sprintf('%.10f', $micro)); echo "Flatten2: " . $final . "\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 11
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
2 jumps found. (Code = 44) Position 1 = 62, Position 2 = 56
Branch analysis from position: 62
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 56
2 jumps found. (Code = 44) Position 1 = 62, Position 2 = 56
Branch analysis from position: 62
Branch analysis from position: 56
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 11
Branch analysis from position: 17
Branch analysis from position: 11
filename:       /in/3db8j
function name:  (null)
number of ops:  96
compiled vars:  !0 = $input, !1 = $start, !2 = $x, !3 = $end, !4 = $diff, !5 = $sec, !6 = $micro, !7 = $final
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   11     1        DECLARE_CLASS                                            'traversableinput'
   27     2        NEW                                              $9      'TraversableInput'
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $9
   57     5        INIT_FCALL                                               'microtime'
          6        SEND_VAL                                                 <true>
          7        DO_ICALL                                         $12     
          8        ASSIGN                                                   !1, $12
   58     9        ASSIGN                                                   !2, 0
         10      > JMP                                                      ->15
   59    11    >   INIT_FCALL                                               'flatten1'
         12        SEND_VAR                                                 !0
         13        DO_FCALL                                      0          
   58    14        PRE_INC                                                  !2
         15    >   IS_SMALLER                                               !2, 10000
         16      > JMPNZ                                                    ~17, ->11
   61    17    >   INIT_FCALL                                               'microtime'
         18        SEND_VAL                                                 <true>
         19        DO_ICALL                                         $18     
         20        ASSIGN                                                   !3, $18
   63    21        SUB                                              ~20     !3, !1
         22        ASSIGN                                                   !4, ~20
   64    23        CAST                                          4  ~22     !4
         24        ASSIGN                                                   !5, ~22
   65    25        SUB                                              ~24     !4, !5
         26        ASSIGN                                                   !6, ~24
   66    27        INIT_FCALL                                               'strftime'
         28        SEND_VAL                                                 '%25T'
         29        INIT_FCALL                                               'mktime'
         30        SEND_VAL                                                 0
         31        SEND_VAL                                                 0
         32        SEND_VAR                                                 !5
         33        DO_ICALL                                         $26     
         34        SEND_VAR                                                 $26
         35        DO_FCALL_BY_NAME                                         
         36        INIT_FCALL                                               'str_replace'
         37        SEND_VAL                                                 '0.'
         38        SEND_VAL                                                 '.'
         39        INIT_FCALL                                               'sprintf'
         40        SEND_VAL                                                 '%25.10f'
         41        SEND_VAR                                                 !6
         42        DO_ICALL                                         $28     
         43        SEND_VAR                                                 $28
         44        DO_ICALL                                         $29     
         45        CONCAT                                           ~30     $27, $29
         46        ASSIGN                                                   !7, ~30
   68    47        CONCAT                                           ~32     'Flatten1%3A+', !7
         48        CONCAT                                           ~33     ~32, '%0A'
         49        ECHO                                                     ~33
   70    50        INIT_FCALL                                               'microtime'
         51        SEND_VAL                                                 <true>
         52        DO_ICALL                                         $34     
         53        ASSIGN                                                   !1, $34
   71    54        ASSIGN                                                   !2, 0
         55      > JMP                                                      ->60
   72    56    >   INIT_FCALL                                               'flatten2'
         57        SEND_VAR                                                 !0
         58        DO_FCALL                                      0          
   71    59        PRE_INC                                                  !2
         60    >   IS_SMALLER                                               !2, 10000
         61      > JMPNZ                                                    ~39, ->56
   74    62    >   INIT_FCALL                                               'microtime'
         63        SEND_VAL                                                 <true>
         64        DO_ICALL                                         $40     
         65        ASSIGN                                                   !3, $40
   76    66        SUB                                              ~42     !3, !1
         67        ASSIGN                                                   !4, ~42
   77    68        CAST                                          4  ~44     !4
         69        ASSIGN                                                   !5, ~44
   78    70        SUB                                              ~46     !4, !5
         71        ASSIGN                                                   !6, ~46
   79    72        INIT_FCALL                                               'strftime'
         73        SEND_VAL                                                 '%25T'
         74        INIT_FCALL                                               'mktime'
         75        SEND_VAL                                                 0
         76        SEND_VAL                                                 0
         77        SEND_VAR                                                 !5
         78        DO_ICALL                                         $48     
         79        SEND_VAR                                                 $48
         80        DO_FCALL_BY_NAME                                         
         81        INIT_FCALL                                               'str_replace'
         82        SEND_VAL                                                 '0.'
         83        SEND_VAL                                                 '.'
         84        INIT_FCALL                                               'sprintf'
         85        SEND_VAL                                                 '%25.10f'
         86        SEND_VAR                                                 !6
         87        DO_ICALL                                         $50     
         88        SEND_VAR                                                 $50
         89        DO_ICALL                                         $51     
         90        CONCAT                                           ~52     $49, $51
         91        ASSIGN                                                   !7, ~52
   81    92        CONCAT                                           ~54     'Flatten2%3A+', !7
         93        CONCAT                                           ~55     ~54, '%0A'
         94        ECHO                                                     ~55
         95      > RETURN                                                   1

Function flatten1:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3db8j
function name:  flatten1
number of ops:  9
compiled vars:  !0 = $input
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
   30     1        INIT_FCALL                                               'array_reduce'
   31     2        SEND_VAR                                                 !0
   32     3        DECLARE_LAMBDA_FUNCTION                          ~1      [0]
   36     4        SEND_VAL                                                 ~1
   37     5        SEND_VAL                                                 <array>
   30     6        DO_ICALL                                         $2      
   37     7      > RETURN                                                   $2
   39     8*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 13
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3db8j
function name:  {closure}
number of ops:  21
compiled vars:  !0 = $carry, !1 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   33     2        TYPE_CHECK                                  128          !1
          3      > JMPZ                                                     ~2, ->13
   34     4    >   INIT_FCALL                                               'array_merge'
          5        SEND_VAR                                                 !0
          6        INIT_FCALL_BY_NAME                                       'flatten1'
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0  $3      
          9        SEND_VAR                                                 $3
         10        DO_ICALL                                         $4      
         11        QM_ASSIGN                                        ~5      $4
         12      > JMP                                                      ->19
   35    13    >   INIT_FCALL                                               'array_merge'
         14        SEND_VAR                                                 !0
         15        INIT_ARRAY                                       ~6      !1
         16        SEND_VAL                                                 ~6
         17        DO_ICALL                                         $7      
         18        QM_ASSIGN                                        ~5      $7
         19    > > RETURN                                                   ~5
   36    20*     > RETURN                                                   null

End of Dynamic Function 0

End of function flatten1

Function flatten2:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 5
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 47) Position 1 = 11, Position 2 = 13
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 23
Branch analysis from position: 14
2 jumps found. (Code = 77) Position 1 = 15, Position 2 = 21
Branch analysis from position: 15
2 jumps found. (Code = 78) Position 1 = 16, Position 2 = 21
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 21
Branch analysis from position: 23
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 5
Branch analysis from position: 30
Branch analysis from position: 5
Branch analysis from position: 13
filename:       /in/3db8j
function name:  flatten2
number of ops:  32
compiled vars:  !0 = $input, !1 = $stack, !2 = $result, !3 = $item, !4 = $element
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   42     1        INIT_ARRAY                                       ~5      !0
          2        ASSIGN                                                   !1, ~5
   43     3        ASSIGN                                                   !2, <array>
   44     4      > JMP                                                      ->27
   45     5    >   INIT_FCALL                                               'array_shift'
          6        SEND_REF                                                 !1
          7        DO_ICALL                                         $8      
          8        ASSIGN                                                   !3, $8
   46     9        TYPE_CHECK                                  128  ~10     !3
         10      > JMPNZ_EX                                         ~10     ~10, ->13
         11    >   INSTANCEOF                                       ~11     !3, 'Traversable'
         12        BOOL                                             ~10     ~11
         13    > > JMPZ                                                     ~10, ->23
   47    14    > > FE_RESET_R                                       $12     !3, ->21
         15    > > FE_FETCH_R                                               $12, !4, ->21
   48    16    >   INIT_FCALL                                               'array_unshift'
         17        SEND_REF                                                 !1
         18        SEND_VAR                                                 !4
         19        DO_ICALL                                                 
   47    20      > JMP                                                      ->15
         21    >   FE_FREE                                                  $12
   46    22      > JMP                                                      ->27
   51    23    >   INIT_FCALL                                               'array_unshift'
         24        SEND_REF                                                 !2
         25        SEND_VAR                                                 !3
         26        DO_ICALL                                                 
   44    27    >   ISSET_ISEMPTY_CV                                 ~15     !1
         28        BOOL_NOT                                         ~16     ~15
         29      > JMPNZ                                                    ~16, ->5
   54    30    > > RETURN                                                   !2
   55    31*     > RETURN                                                   null

End of function flatten2

Class TraversableInput:
Function getiterator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/3db8j
function name:  getIterator
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   NEW                                              $0      'ArrayIterator'
          1        CHECK_FUNC_ARG                                           
          2        FETCH_OBJ_FUNC_ARG                               $1      'input'
          3        SEND_FUNC_ARG                                            $1
          4        DO_FCALL                                      0          
          5      > RETURN                                                   $0
   24     6*     > RETURN                                                   null

End of function getiterator

End of class TraversableInput.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.51 ms | 1481 KiB | 24 Q