3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface BarInterface{} function getMemoryUsage($size){ static $unit=['b','kb','mb','gb','tb','pb']; return $size > 0 ? @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i] : 0 . ' byte'; } class Bar implements BarInterface { public function run() { return true; } } class Foo { public function variadic(\BarInterface ...$bars) { foreach ($bars as $bar) { $bar->run(); } } public function iterated(array $bars) { foreach ($bars as $bar) { if (!$bar instanceof \BarInterface) { throw new \TypeError('Invalid BarInterface specified'); } } foreach ($bars as $bar) { $bar->run(); } } } $bars = []; for ($i=0; $i<10000 ;$i++) { $bars[] = new \Bar(); } $foo = new Foo(); $items = \count($bars); //initial call to reduce overhead $foo->variadic(...[]); $foo->iterated([]); microtime(true); memory_get_usage(true); //benchmark start $start = new \DateTime(); $mem_start = memory_get_usage(true); $foo->variadic(...$bars); $mem_variadic = memory_get_usage(true); $variadic = new \DateTime(); $variadicDiff = $start->diff($variadic)->format('%f'); printf('%sms (variadic passed %d %s increase)', $variadicDiff, $items, getMemoryUsage($mem_variadic-$mem_start)); echo \PHP_EOL . '--------' . \PHP_EOL; $start = new \DateTime(); $mem_start = memory_get_usage(true); $foo->iterated($bars); $mem_iterated = memory_get_usage(true); $iterated = new \DateTime(); $iteratedDiff = $start->diff($iterated)->format('%f'); printf('%sms (iterated passed %d %s increase)', $iteratedDiff, $items, getMemoryUsage($mem_iterated-$mem_start)); echo \PHP_EOL . '--------' . \PHP_EOL; if ($variadicDiff > $iteratedDiff) { $loser = 'Variadic'; $diff = $variadicDiff - $iteratedDiff; $loss = $diff / $iteratedDiff; } else { $loser = 'Iterated'; $diff = $iteratedDiff - $variadicDiff; $loss = $diff / $variadicDiff; } printf('%s was slower by %.2f%% (%d ms)', $loser, $loss * 100, $diff);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 4
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 102, Position 2 = 108
Branch analysis from position: 102
1 jumps found. (Code = 42) Position 1 = 113
Branch analysis from position: 113
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 108
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 4
Branch analysis from position: 11
Branch analysis from position: 4
filename:       /in/JMrsq
function name:  (null)
number of ops:  121
compiled vars:  !0 = $bars, !1 = $i, !2 = $foo, !3 = $items, !4 = $start, !5 = $mem_start, !6 = $mem_variadic, !7 = $variadic, !8 = $variadicDiff, !9 = $mem_iterated, !10 = $iterated, !11 = $iteratedDiff, !12 = $loser, !13 = $diff, !14 = $loss
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   DECLARE_CLASS                                            'bar'
   40     1        ASSIGN                                                   !0, <array>
   41     2        ASSIGN                                                   !1, 0
          3      > JMP                                                      ->9
   42     4    >   NEW                                              $18     'Bar'
          5        DO_FCALL                                      0          
          6        ASSIGN_DIM                                               !0
          7        OP_DATA                                                  $18
   41     8        PRE_INC                                                  !1
          9    >   IS_SMALLER                                               !1, 10000
         10      > JMPNZ                                                    ~21, ->4
   44    11    >   NEW                                              $22     'Foo'
         12        DO_FCALL                                      0          
         13        ASSIGN                                                   !2, $22
   45    14        COUNT                                            ~25     !0
         15        ASSIGN                                                   !3, ~25
   47    16        INIT_METHOD_CALL                                         !2, 'variadic'
         17        SEND_UNPACK                                              <array>
         18        CHECK_UNDEF_ARGS                                         
         19        DO_FCALL                                      1          
   48    20        INIT_METHOD_CALL                                         !2, 'iterated'
         21        SEND_VAL_EX                                              <array>
         22        DO_FCALL                                      0          
   49    23        INIT_FCALL                                               'microtime'
         24        SEND_VAL                                                 <true>
         25        DO_ICALL                                                 
   50    26        INIT_FCALL                                               'memory_get_usage'
         27        SEND_VAL                                                 <true>
         28        DO_ICALL                                                 
   53    29        NEW                                              $31     'DateTime'
         30        DO_FCALL                                      0          
         31        ASSIGN                                                   !4, $31
   54    32        INIT_FCALL                                               'memory_get_usage'
         33        SEND_VAL                                                 <true>
         34        DO_ICALL                                         $34     
         35        ASSIGN                                                   !5, $34
   55    36        INIT_METHOD_CALL                                         !2, 'variadic'
         37        SEND_UNPACK                                              !0
         38        CHECK_UNDEF_ARGS                                         
         39        DO_FCALL                                      1          
   56    40        INIT_FCALL                                               'memory_get_usage'
         41        SEND_VAL                                                 <true>
         42        DO_ICALL                                         $37     
         43        ASSIGN                                                   !6, $37
   57    44        NEW                                              $39     'DateTime'
         45        DO_FCALL                                      0          
         46        ASSIGN                                                   !7, $39
   58    47        INIT_METHOD_CALL                                         !4, 'diff'
         48        SEND_VAR_EX                                              !7
         49        DO_FCALL                                      0  $42     
         50        INIT_METHOD_CALL                                         $42, 'format'
         51        SEND_VAL_EX                                              '%25f'
         52        DO_FCALL                                      0  $43     
         53        ASSIGN                                                   !8, $43
   59    54        INIT_FCALL                                               'printf'
         55        SEND_VAL                                                 '%25sms+%28variadic+passed+%25d+%25s+increase%29'
         56        SEND_VAR                                                 !8
         57        SEND_VAR                                                 !3
         58        INIT_FCALL                                               'getmemoryusage'
         59        SUB                                              ~45     !6, !5
         60        SEND_VAL                                                 ~45
         61        DO_FCALL                                      0  $46     
         62        SEND_VAR                                                 $46
         63        DO_ICALL                                                 
   60    64        ECHO                                                     '%0A--------%0A'
   62    65        NEW                                              $48     'DateTime'
         66        DO_FCALL                                      0          
         67        ASSIGN                                                   !4, $48
   63    68        INIT_FCALL                                               'memory_get_usage'
         69        SEND_VAL                                                 <true>
         70        DO_ICALL                                         $51     
         71        ASSIGN                                                   !5, $51
   64    72        INIT_METHOD_CALL                                         !2, 'iterated'
         73        SEND_VAR_EX                                              !0
         74        DO_FCALL                                      0          
   65    75        INIT_FCALL                                               'memory_get_usage'
         76        SEND_VAL                                                 <true>
         77        DO_ICALL                                         $54     
         78        ASSIGN                                                   !9, $54
   66    79        NEW                                              $56     'DateTime'
         80        DO_FCALL                                      0          
         81        ASSIGN                                                   !10, $56
   67    82        INIT_METHOD_CALL                                         !4, 'diff'
         83        SEND_VAR_EX                                              !10
         84        DO_FCALL                                      0  $59     
         85        INIT_METHOD_CALL                                         $59, 'format'
         86        SEND_VAL_EX                                              '%25f'
         87        DO_FCALL                                      0  $60     
         88        ASSIGN                                                   !11, $60
   68    89        INIT_FCALL                                               'printf'
         90        SEND_VAL                                                 '%25sms+%28iterated+passed+%25d+%25s+increase%29'
         91        SEND_VAR                                                 !11
         92        SEND_VAR                                                 !3
         93        INIT_FCALL                                               'getmemoryusage'
         94        SUB                                              ~62     !9, !5
         95        SEND_VAL                                                 ~62
         96        DO_FCALL                                      0  $63     
         97        SEND_VAR                                                 $63
         98        DO_ICALL                                                 
   69    99        ECHO                                                     '%0A--------%0A'
   71   100        IS_SMALLER                                               !11, !8
        101      > JMPZ                                                     ~65, ->108
   72   102    >   ASSIGN                                                   !12, 'Variadic'
   73   103        SUB                                              ~67     !8, !11
        104        ASSIGN                                                   !13, ~67
   74   105        DIV                                              ~69     !13, !11
        106        ASSIGN                                                   !14, ~69
        107      > JMP                                                      ->113
   76   108    >   ASSIGN                                                   !12, 'Iterated'
   77   109        SUB                                              ~72     !11, !8
        110        ASSIGN                                                   !13, ~72
   78   111        DIV                                              ~74     !13, !8
        112        ASSIGN                                                   !14, ~74
   80   113    >   INIT_FCALL                                               'printf'
        114        SEND_VAL                                                 '%25s+was+slower+by+%25.2f%25%25+%28%25d+ms%29'
        115        SEND_VAR                                                 !12
        116        MUL                                              ~76     !14, 100
        117        SEND_VAL                                                 ~76
        118        SEND_VAR                                                 !13
        119        DO_ICALL                                                 
        120      > RETURN                                                   1

Function getmemoryusage:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 28
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/JMrsq
function name:  getMemoryUsage
number of ops:  31
compiled vars:  !0 = $size, !1 = $unit, !2 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
    6     1        BIND_STATIC                                              !1
    7     2        IS_SMALLER                                               0, !0
          3      > JMPZ                                                     ~3, ->28
          4    >   BEGIN_SILENCE                                    ~4      
          5        INIT_FCALL                                               'round'
          6        INIT_FCALL                                               'pow'
          7        SEND_VAL                                                 1024
          8        INIT_FCALL                                               'floor'
          9        INIT_FCALL                                               'log'
         10        SEND_VAR                                                 !0
         11        SEND_VAL                                                 1024
         12        DO_ICALL                                         $5      
         13        SEND_VAR                                                 $5
         14        DO_ICALL                                         $6      
         15        ASSIGN                                           ~7      !2, $6
         16        SEND_VAL                                                 ~7
         17        DO_ICALL                                         $8      
         18        DIV                                              ~9      !0, $8
         19        SEND_VAL                                                 ~9
         20        SEND_VAL                                                 2
         21        DO_ICALL                                         $10     
         22        END_SILENCE                                              ~4
         23        CONCAT                                           ~11     $10, '+'
         24        FETCH_DIM_R                                      ~12     !1, !2
         25        CONCAT                                           ~13     ~11, ~12
         26        QM_ASSIGN                                        ~14     ~13
         27      > JMP                                                      ->29
         28    >   QM_ASSIGN                                        ~14     '0+byte'
         29    > > RETURN                                                   ~14
    8    30*     > RETURN                                                   null

End of function getmemoryusage

Class BarInterface: [no user functions]
Class Bar:
Function run:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/JMrsq
function name:  run
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E > > RETURN                                                   <true>
   15     1*     > RETURN                                                   null

End of function run

End of class Bar.

Class Foo:
Function variadic:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 6
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
filename:       /in/JMrsq
function name:  variadic
number of ops:  8
compiled vars:  !0 = $bars, !1 = $bar
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV_VARIADIC                                    !0      
   22     1      > FE_RESET_R                                       $2      !0, ->6
          2    > > FE_FETCH_R                                               $2, !1, ->6
   23     3    >   INIT_METHOD_CALL                                         !1, 'run'
          4        DO_FCALL                                      0          
   22     5      > JMP                                                      ->2
          6    >   FE_FREE                                                  $2
   25     7      > RETURN                                                   null

End of function variadic

Function iterated:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 11
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 11
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 10
Branch analysis from position: 6
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 17
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
Branch analysis from position: 11
filename:       /in/JMrsq
function name:  iterated
number of ops:  19
compiled vars:  !0 = $bars, !1 = $bar
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   29     1      > FE_RESET_R                                       $2      !0, ->11
          2    > > FE_FETCH_R                                               $2, !1, ->11
   30     3    >   INSTANCEOF                                       ~3      !1, 'BarInterface'
          4        BOOL_NOT                                         ~4      ~3
          5      > JMPZ                                                     ~4, ->10
   31     6    >   NEW                                              $5      'TypeError'
          7        SEND_VAL_EX                                              'Invalid+BarInterface+specified'
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $5
   29    10    > > JMP                                                      ->2
         11    >   FE_FREE                                                  $2
   34    12      > FE_RESET_R                                       $7      !0, ->17
         13    > > FE_FETCH_R                                               $7, !1, ->17
   35    14    >   INIT_METHOD_CALL                                         !1, 'run'
         15        DO_FCALL                                      0          
   34    16      > JMP                                                      ->13
         17    >   FE_FREE                                                  $7
   37    18      > RETURN                                                   null

End of function iterated

End of class Foo.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.08 ms | 1415 KiB | 29 Q