3v4l.org

run code in 300+ PHP versions simultaneously
<?php if (PHP_VERSION !== '7.0.0RC4') { exit('done'); } class Bench { protected $functions = []; public function add($label, \Closure $function) { $this->functions[] = [$label, $function]; } public function run($count) { $function = function () {}; $stats = $this->getStats(); for ($i = $count; $i--;) $function(); $stats2 = $this->getStats(); $timeDiff = $stats2[4] - $stats[4]; $this->printStats('Empty run (time subtracted from real runs)', $stats, $stats2, 0); // TODO: Split runs in pieces and interleave, to avoid bias with first/last runner. foreach ($this->functions as list($label, $function)) { $stats = $this->getStats(); for ($i = $count; $i--;) $function(); $stats2 = $this->getStats(); $this->printStats($label, $stats, $stats2, $timeDiff); } } protected function getStats() { return [memory_get_usage(), memory_get_usage(true), memory_get_peak_usage(), memory_get_peak_usage(true), microtime(1)]; } protected function printStats($label, $stats1, $stats2, $timeDiff) { list($mem, $memReal, $memPeak, $memRealPeak, $time) = $stats1; list($memNew, $memRealNew, $memPeakNew, $memRealPeakNew, $timeNew) = $stats2; $nl = "\n"; $labelLength = 20; $resultsLength = 15; echo '<p><b>' . $label . ':</b></p>' . $nl; echo '<code><p>' . $nl; echo str_pad('Memory', $labelLength, '.', STR_PAD_RIGHT); echo str_pad(number_format(($memNew - $mem) >> 10, 0), $resultsLength, '.', STR_PAD_LEFT) . ' kb<br>' . $nl; echo str_pad('Memory Real', $labelLength, '.', STR_PAD_RIGHT); echo str_pad(number_format(($memRealNew - $memReal) >> 10, 0), $resultsLength, '.', STR_PAD_LEFT) . ' kb<br>' . $nl; echo str_pad('Memory Peak', $labelLength, '.', STR_PAD_RIGHT); echo str_pad(number_format(($memPeakNew - $memPeak) >> 10, 0), $resultsLength, '.', STR_PAD_LEFT) . ' kb<br>' . $nl; echo str_pad('Memory Real Peak', $labelLength, '.', STR_PAD_RIGHT); echo str_pad(number_format(($memRealPeakNew - $memRealPeak) >> 10, 0), $resultsLength, '.', STR_PAD_LEFT) . ' kb<br>' . $nl; echo str_pad('Time', $labelLength, '.', STR_PAD_RIGHT); echo str_pad(number_format($timeNew - $time - $timeDiff, 5), $resultsLength, '.', STR_PAD_LEFT); echo ' sec' . $nl . '</code></p>' . $nl . $nl; } } $f1 = function ($input) { return $input * 10; }; $f2 = function ($input) { return [true, $input * 10]; }; $b = new Bench(); $b->add('single', function () use ($f1) { $value = $f1(10); }); $b->add('tuple', function () use ($f2) { list($error, $value) = $f2(10); }); $b->run(40000);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 1, Position 2 = 2
Branch analysis from position: 1
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 2
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pbrjh
function name:  (null)
number of ops:  25
compiled vars:  !0 = $f1, !1 = $f2, !2 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E > > JMPZ                                                     <true>, ->2
          1    > > EXIT                                                     'done'
   61     2    >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fpbrjh%3A61%241'
          3        ASSIGN                                                   !0, ~3
   65     4        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fpbrjh%3A65%242'
          5        ASSIGN                                                   !1, ~5
   69     6        NEW                                              $7      'Bench'
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !2, $7
   70     9        INIT_METHOD_CALL                                         !2, 'add'
         10        SEND_VAL_EX                                              'single'
         11        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fpbrjh%3A70%243'
         12        BIND_LEXICAL                                             ~10, !0
   72    13        SEND_VAL_EX                                              ~10
         14        DO_FCALL                                      0          
   73    15        INIT_METHOD_CALL                                         !2, 'add'
         16        SEND_VAL_EX                                              'tuple'
         17        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fpbrjh%3A73%244'
         18        BIND_LEXICAL                                             ~12, !1
   75    19        SEND_VAL_EX                                              ~12
         20        DO_FCALL                                      0          
   76    21        INIT_METHOD_CALL                                         !2, 'run'
         22        SEND_VAL_EX                                              40000
         23        DO_FCALL                                      0          
         24      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2Fpbrjh%3A12%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pbrjh
function name:  {closure}
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E > > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fpbrjh%3A12%240

Function %00%7Bclosure%7D%2Fin%2Fpbrjh%3A61%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pbrjh
function name:  {closure}
number of ops:  4
compiled vars:  !0 = $input
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
   62     1        MUL                                              ~1      !0, 10
          2      > RETURN                                                   ~1
   63     3*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fpbrjh%3A61%241

Function %00%7Bclosure%7D%2Fin%2Fpbrjh%3A65%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pbrjh
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $input
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   RECV                                             !0      
   66     1        INIT_ARRAY                                       ~1      <true>
          2        MUL                                              ~2      !0, 10
          3        ADD_ARRAY_ELEMENT                                ~1      ~2
          4      > RETURN                                                   ~1
   67     5*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fpbrjh%3A65%242

Function %00%7Bclosure%7D%2Fin%2Fpbrjh%3A70%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pbrjh
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $f1, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   BIND_STATIC                                              !0
   71     1        INIT_DYNAMIC_CALL                                        !0
          2        SEND_VAL_EX                                              10
          3        DO_FCALL                                      0  $2      
          4        ASSIGN                                                   !1, $2
   72     5      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fpbrjh%3A70%243

Function %00%7Bclosure%7D%2Fin%2Fpbrjh%3A73%244:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pbrjh
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $f2, !1 = $error, !2 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   BIND_STATIC                                              !0
   74     1        INIT_DYNAMIC_CALL                                        !0
          2        SEND_VAL_EX                                              10
          3        DO_FCALL                                      0  $3      
          4        FETCH_LIST_R                                     $4      $3, 0
          5        ASSIGN                                                   !1, $4
          6        FETCH_LIST_R                                     $6      $3, 1
          7        ASSIGN                                                   !2, $6
          8        FREE                                                     $3
   75     9      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fpbrjh%3A73%244

Class Bench:
Function add:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pbrjh
function name:  add
number of ops:  8
compiled vars:  !0 = $label, !1 = $function
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    8     2        INIT_ARRAY                                       ~4      !0
          3        ADD_ARRAY_ELEMENT                                ~4      !1
          4        FETCH_OBJ_W                                      $2      'functions'
          5        ASSIGN_DIM                                               $2
          6        OP_DATA                                                  ~4
    9     7      > RETURN                                                   null

End of function add

Function run:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 12, Position 2 = 8
Branch analysis from position: 12
2 jumps found. (Code = 77) Position 1 = 27, Position 2 = 52
Branch analysis from position: 27
2 jumps found. (Code = 78) Position 1 = 28, Position 2 = 52
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
2 jumps found. (Code = 44) Position 1 = 42, Position 2 = 38
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 42, Position 2 = 38
Branch analysis from position: 42
Branch analysis from position: 38
Branch analysis from position: 52
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 52
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 12, Position 2 = 8
Branch analysis from position: 12
Branch analysis from position: 8
filename:       /in/pbrjh
function name:  run
number of ops:  54
compiled vars:  !0 = $count, !1 = $function, !2 = $stats, !3 = $i, !4 = $stats2, !5 = $timeDiff, !6 = $label
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
   12     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fpbrjh%3A12%240'
          2        ASSIGN                                                   !1, ~7
   14     3        INIT_METHOD_CALL                                         'getStats'
          4        DO_FCALL                                      0  $9      
          5        ASSIGN                                                   !2, $9
   15     6        ASSIGN                                                   !3, !0
          7      > JMP                                                      ->10
          8    >   INIT_DYNAMIC_CALL                                        !1
          9        DO_FCALL                                      0          
         10    >   POST_DEC                                         ~13     !3
         11      > JMPNZ                                                    ~13, ->8
   16    12    >   INIT_METHOD_CALL                                         'getStats'
         13        DO_FCALL                                      0  $14     
         14        ASSIGN                                                   !4, $14
   18    15        FETCH_DIM_R                                      ~16     !4, 4
         16        FETCH_DIM_R                                      ~17     !2, 4
         17        SUB                                              ~18     ~16, ~17
         18        ASSIGN                                                   !5, ~18
   20    19        INIT_METHOD_CALL                                         'printStats'
         20        SEND_VAL_EX                                              'Empty+run+%28time+subtracted+from+real+runs%29'
         21        SEND_VAR_EX                                              !2
         22        SEND_VAR_EX                                              !4
         23        SEND_VAL_EX                                              0
         24        DO_FCALL                                      0          
   23    25        FETCH_OBJ_R                                      ~21     'functions'
         26      > FE_RESET_R                                       $22     ~21, ->52
         27    > > FE_FETCH_R                                               $22, $23, ->52
         28    >   FETCH_LIST_R                                     $24     $23, 0
         29        ASSIGN                                                   !6, $24
         30        FETCH_LIST_R                                     $26     $23, 1
         31        ASSIGN                                                   !1, $26
         32        FREE                                                     $23
   24    33        INIT_METHOD_CALL                                         'getStats'
         34        DO_FCALL                                      0  $28     
         35        ASSIGN                                                   !2, $28
   26    36        ASSIGN                                                   !3, !0
         37      > JMP                                                      ->40
         38    >   INIT_DYNAMIC_CALL                                        !1
         39        DO_FCALL                                      0          
         40    >   POST_DEC                                         ~32     !3
         41      > JMPNZ                                                    ~32, ->38
   28    42    >   INIT_METHOD_CALL                                         'getStats'
         43        DO_FCALL                                      0  $33     
         44        ASSIGN                                                   !4, $33
   29    45        INIT_METHOD_CALL                                         'printStats'
         46        SEND_VAR_EX                                              !6
         47        SEND_VAR_EX                                              !2
         48        SEND_VAR_EX                                              !4
         49        SEND_VAR_EX                                              !5
         50        DO_FCALL                                      0          
   23    51      > JMP                                                      ->27
         52    >   FE_FREE                                                  $22
   31    53      > RETURN                                                   null

End of function run

Function getstats:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pbrjh
function name:  getStats
number of ops:  20
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   INIT_FCALL                                               'memory_get_usage'
          1        DO_ICALL                                         $0      
          2        INIT_ARRAY                                       ~1      $0
          3        INIT_FCALL                                               'memory_get_usage'
          4        SEND_VAL                                                 <true>
          5        DO_ICALL                                         $2      
          6        ADD_ARRAY_ELEMENT                                ~1      $2
          7        INIT_FCALL                                               'memory_get_peak_usage'
          8        DO_ICALL                                         $3      
          9        ADD_ARRAY_ELEMENT                                ~1      $3
         10        INIT_FCALL                                               'memory_get_peak_usage'
         11        SEND_VAL                                                 <true>
         12        DO_ICALL                                         $4      
         13        ADD_ARRAY_ELEMENT                                ~1      $4
         14        INIT_FCALL                                               'microtime'
         15        SEND_VAL                                                 1
         16        DO_ICALL                                         $5      
         17        ADD_ARRAY_ELEMENT                                ~1      $5
         18      > RETURN                                                   ~1
   35    19*     > RETURN                                                   null

End of function getstats

Function printstats:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pbrjh
function name:  printStats
number of ops:  151
compiled vars:  !0 = $label, !1 = $stats1, !2 = $stats2, !3 = $timeDiff, !4 = $mem, !5 = $memReal, !6 = $memPeak, !7 = $memRealPeak, !8 = $time, !9 = $memNew, !10 = $memRealNew, !11 = $memPeakNew, !12 = $memRealPeakNew, !13 = $timeNew, !14 = $nl, !15 = $labelLength, !16 = $resultsLength
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   38     4        QM_ASSIGN                                        ~17     !1
          5        FETCH_LIST_R                                     $18     ~17, 0
          6        ASSIGN                                                   !4, $18
          7        FETCH_LIST_R                                     $20     ~17, 1
          8        ASSIGN                                                   !5, $20
          9        FETCH_LIST_R                                     $22     ~17, 2
         10        ASSIGN                                                   !6, $22
         11        FETCH_LIST_R                                     $24     ~17, 3
         12        ASSIGN                                                   !7, $24
         13        FETCH_LIST_R                                     $26     ~17, 4
         14        ASSIGN                                                   !8, $26
         15        FREE                                                     ~17
   39    16        QM_ASSIGN                                        ~28     !2
         17        FETCH_LIST_R                                     $29     ~28, 0
         18        ASSIGN                                                   !9, $29
         19        FETCH_LIST_R                                     $31     ~28, 1
         20        ASSIGN                                                   !10, $31
         21        FETCH_LIST_R                                     $33     ~28, 2
         22        ASSIGN                                                   !11, $33
         23        FETCH_LIST_R                                     $35     ~28, 3
         24        ASSIGN                                                   !12, $35
         25        FETCH_LIST_R                                     $37     ~28, 4
         26        ASSIGN                                                   !13, $37
         27        FREE                                                     ~28
   41    28        ASSIGN                                                   !14, '%0A'
   43    29        ASSIGN                                                   !15, 20
   44    30        ASSIGN                                                   !16, 15
   45    31        CONCAT                                           ~42     '%3Cp%3E%3Cb%3E', !0
         32        CONCAT                                           ~43     ~42, '%3A%3C%2Fb%3E%3C%2Fp%3E'
         33        CONCAT                                           ~44     ~43, !14
         34        ECHO                                                     ~44
   46    35        CONCAT                                           ~45     '%3Ccode%3E%3Cp%3E', !14
         36        ECHO                                                     ~45
   47    37        INIT_FCALL                                               'str_pad'
         38        SEND_VAL                                                 'Memory'
         39        SEND_VAR                                                 !15
         40        SEND_VAL                                                 '.'
         41        SEND_VAL                                                 1
         42        DO_ICALL                                         $46     
         43        ECHO                                                     $46
   48    44        INIT_FCALL                                               'str_pad'
         45        INIT_FCALL                                               'number_format'
         46        SUB                                              ~47     !9, !4
         47        SR                                               ~48     ~47, 10
         48        SEND_VAL                                                 ~48
         49        SEND_VAL                                                 0
         50        DO_ICALL                                         $49     
         51        SEND_VAR                                                 $49
         52        SEND_VAR                                                 !16
         53        SEND_VAL                                                 '.'
         54        SEND_VAL                                                 0
         55        DO_ICALL                                         $50     
         56        CONCAT                                           ~51     $50, '+kb%3Cbr%3E'
         57        CONCAT                                           ~52     ~51, !14
         58        ECHO                                                     ~52
   49    59        INIT_FCALL                                               'str_pad'
         60        SEND_VAL                                                 'Memory+Real'
         61        SEND_VAR                                                 !15
         62        SEND_VAL                                                 '.'
         63        SEND_VAL                                                 1
         64        DO_ICALL                                         $53     
         65        ECHO                                                     $53
   50    66        INIT_FCALL                                               'str_pad'
         67        INIT_FCALL                                               'number_format'
         68        SUB                                              ~54     !10, !5
         69        SR                                               ~55     ~54, 10
         70        SEND_VAL                                                 ~55
         71        SEND_VAL                                                 0
         72        DO_ICALL                                         $56     
         73        SEND_VAR                                                 $56
         74        SEND_VAR                                                 !16
         75        SEND_VAL                                                 '.'
         76        SEND_VAL                                                 0
         77        DO_ICALL                                         $57     
         78        CONCAT                                           ~58     $57, '+kb%3Cbr%3E'
         79        CONCAT                                           ~59     ~58, !14
         80        ECHO                                                     ~59
   51    81        INIT_FCALL                                               'str_pad'
         82        SEND_VAL                                                 'Memory+Peak'
         83        SEND_VAR                                                 !15
         84        SEND_VAL                                                 '.'
         85        SEND_VAL                                                 1
         86        DO_ICALL                                         $60     
         87        ECHO                                                     $60
   52    88        INIT_FCALL                                               'str_pad'
         89        INIT_FCALL                                               'number_format'
         90        SUB                                              ~61     !11, !6
         91        SR                                               ~62     ~61, 10
         92        SEND_VAL                                                 ~62
         93        SEND_VAL                                                 0
         94        DO_ICALL                                         $63     
         95        SEND_VAR                                                 $63
         96        SEND_VAR                                                 !16
         97        SEND_VAL                                                 '.'
         98        SEND_VAL                                                 0
         99        DO_ICALL                                         $64     
        100        CONCAT                                           ~65     $64, '+kb%3Cbr%3E'
        101        CONCAT                                           ~66     ~65, !14
        102        ECHO                                                     ~66
   53   103        INIT_FCALL                                               'str_pad'
        104        SEND_VAL                                                 'Memory+Real+Peak'
        105        SEND_VAR                                                 !15
        106        SEND_VAL                                                 '.'
        107        SEND_VAL                                                 1
        108        DO_ICALL                                         $67     
        109        ECHO                                                     $67
   54   110        INIT_FCALL                                               'str_pad'
        111        INIT_FCALL                                               'number_format'
        112        SUB                                              ~68     !12, !7
        113        SR                                               ~69     ~68, 10
        114        SEND_VAL                                                 ~69
        115        SEND_VAL                                                 0
        116        DO_ICALL                                         $70     
        117        SEND_VAR                                                 $70
        118        SEND_VAR                                                 !16
        119        SEND_VAL                                                 '.'
        120        SEND_VAL                                                 0
        121        DO_ICALL                                         $71     
        122        CONCAT                                           ~72     $71, '+kb%3Cbr%3E'
        123        CONCAT                                           ~73     ~72, !14
        124        ECHO                                                     ~73
   55   125        INIT_FCALL                                               'str_pad'
        126        SEND_VAL                                                 'Time'
        127        SEND_VAR                                                 !15
        128        SEND_VAL                                                 '.'
        129        SEND_VAL                                                 1
        130        DO_ICALL                                         $74     
        131        ECHO                                                     $74
   56   132        INIT_FCALL                                               'str_pad'
        133        INIT_FCALL                                               'number_format'
        134        SUB                                              ~75     !13, !8
        135        SUB                                              ~76     ~75, !3
        136        SEND_VAL                                                 ~76
        137        SEND_VAL                                                 5
        138        DO_ICALL                                         $77     
        139        SEND_VAR                                                 $77
        140        SEND_VAR                                                 !16
        141        SEND_VAL                                                 '.'
        142        SEND_VAL                                                 0
        143        DO_ICALL                                         $78     
        144        ECHO                                                     $78
   57   145        CONCAT                                           ~79     '+sec', !14
        146        CONCAT                                           ~80     ~79, '%3C%2Fcode%3E%3C%2Fp%3E'
        147        CONCAT                                           ~81     ~80, !14
        148        CONCAT                                           ~82     ~81, !14
        149        ECHO                                                     ~82
   58   150      > RETURN                                                   null

End of function printstats

End of class Bench.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.89 ms | 1424 KiB | 23 Q