3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types = 1); /** @noinspection AutoloadingIssuesInspection */ class BenchmarkCase { public static $maxNameLength; public $callable; public $iterations = 0; public $name; public $timing = 0; public static function create(string $name, callable $callable): self { $self = new self; $self->name = $name; $self->callable = $callable; self::$maxNameLength = max(self::$maxNameLength, strlen($name)); return $self; } public static function printResults(float $initialMs, array $cases): void { $durationMs = microtime(true) - $initialMs; printf("\r"); usort($cases, static function (BenchmarkCase $a, BenchmarkCase $b) { return $a->timing <=> $b->timing; }); $caseFirst = $cases[0]->timing; /** @var BenchmarkCase $case */ foreach ($cases as $case) { printf("Case %s duration of %.6fs (avg. each %.8fs) +%.2f%%\n", str_pad($case->name, self::$maxNameLength, ' '), $case->timing, bcdiv((string) $case->timing, (string) $case->iterations, 8), (100 / $caseFirst * $case->timing) - 100); } printf("\nTotal duration: %.6fs", $durationMs); } } set_error_handler(static function ($severity, $message) { throw new ErrorException($message, 0, $severity); }); $cases = [ BenchmarkCase::create('array_merge() inside looping', static function () { $arr = []; for ($i = 0; $i < 100; $i++) { $arr = array_merge($arr, [ $i, $i ]); } return $arr; }), BenchmarkCase::create('array_merge() after looping', static function () { $arrArr = []; for ($i = 0; $i < 100; $i++) { $arrArr[] = [ $i, $i ]; } return array_merge(... $arrArr); }), BenchmarkCase::create('array_push() inside looping', static function () { $arr = []; for ($i = 0; $i < 100; $i++) { array_push($arr, $i, $i); } return $arr; }), ]; const BENCHMARK_DURATION_MS = 1500; const CYCLES_ITERATION = 100; $initialMs = microtime(true); $stopTiming = $initialMs + BENCHMARK_DURATION_MS / 1000; while (($currentTiming = microtime(true)) < $stopTiming) { /** @noinspection NonSecureShuffleUsageInspection */ shuffle($cases); /** @var BenchmarkCase $case */ foreach ($cases as $case) { $startMs = microtime(true); for ($j = 0; $j < CYCLES_ITERATION; $j++) { call_user_func($case->callable); } $case->iterations += CYCLES_ITERATION; $case->timing += microtime(true) - $startMs; } } BenchmarkCase::printResults($initialMs, $cases);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 63
Branch analysis from position: 63
2 jumps found. (Code = 44) Position 1 = 69, Position 2 = 34
Branch analysis from position: 69
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
2 jumps found. (Code = 77) Position 1 = 38, Position 2 = 62
Branch analysis from position: 38
2 jumps found. (Code = 78) Position 1 = 39, Position 2 = 62
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
2 jumps found. (Code = 44) Position 1 = 52, Position 2 = 45
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
Branch analysis from position: 45
2 jumps found. (Code = 44) Position 1 = 52, Position 2 = 45
Branch analysis from position: 52
Branch analysis from position: 45
Branch analysis from position: 62
2 jumps found. (Code = 44) Position 1 = 69, Position 2 = 34
Branch analysis from position: 69
Branch analysis from position: 34
Branch analysis from position: 62
filename:       /in/Vqt7o
function name:  (null)
number of ops:  74
compiled vars:  !0 = $cases, !1 = $initialMs, !2 = $stopTiming, !3 = $case, !4 = $startMs, !5 = $j, !6 = $currentTiming
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   INIT_FCALL                                               'set_error_handler'
          1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FVqt7o%3A56%241'
   58     2        SEND_VAL                                                 ~7
          3        DO_ICALL                                                 
   61     4        INIT_STATIC_METHOD_CALL                                  'BenchmarkCase', 'create'
          5        SEND_VAL                                                 'array_merge%28%29+inside+looping'
          6        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FVqt7o%3A61%242'
   69     7        SEND_VAL                                                 ~9
          8        DO_FCALL                                      0  $10     
          9        INIT_ARRAY                                       ~11     $10
   70    10        INIT_STATIC_METHOD_CALL                                  'BenchmarkCase', 'create'
         11        SEND_VAL                                                 'array_merge%28%29+after+looping'
         12        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FVqt7o%3A70%243'
   78    13        SEND_VAL                                                 ~12
         14        DO_FCALL                                      0  $13     
         15        ADD_ARRAY_ELEMENT                                ~11     $13
   79    16        INIT_STATIC_METHOD_CALL                                  'BenchmarkCase', 'create'
         17        SEND_VAL                                                 'array_push%28%29+inside+looping'
         18        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FVqt7o%3A79%244'
   87    19        SEND_VAL                                                 ~14
         20        DO_FCALL                                      0  $15     
         21        ADD_ARRAY_ELEMENT                                ~11     $15
   60    22        ASSIGN                                                   !0, ~11
   90    23        DECLARE_CONST                                            'BENCHMARK_DURATION_MS', 1500
   91    24        DECLARE_CONST                                            'CYCLES_ITERATION', 100
   93    25        INIT_FCALL                                               'microtime'
         26        SEND_VAL                                                 <true>
         27        DO_ICALL                                         $17     
         28        ASSIGN                                                   !1, $17
   94    29        FETCH_CONSTANT                                   ~19     'BENCHMARK_DURATION_MS'
         30        DIV                                              ~20     ~19, 1000
         31        ADD                                              ~21     !1, ~20
         32        ASSIGN                                                   !2, ~21
   96    33      > JMP                                                      ->63
   98    34    >   INIT_FCALL                                               'shuffle'
         35        SEND_REF                                                 !0
         36        DO_ICALL                                                 
  101    37      > FE_RESET_R                                       $24     !0, ->62
         38    > > FE_FETCH_R                                               $24, !3, ->62
  102    39    >   INIT_FCALL                                               'microtime'
         40        SEND_VAL                                                 <true>
         41        DO_ICALL                                         $25     
         42        ASSIGN                                                   !4, $25
  104    43        ASSIGN                                                   !5, 0
         44      > JMP                                                      ->49
  105    45    >   FETCH_OBJ_R                                      ~28     !3, 'callable'
         46        INIT_USER_CALL                                0          'call_user_func', ~28
         47        DO_FCALL                                      0          
  104    48        PRE_INC                                                  !5
         49    >   FETCH_CONSTANT                                   ~31     'CYCLES_ITERATION'
         50        IS_SMALLER                                               !5, ~31
         51      > JMPNZ                                                    ~32, ->45
  108    52    >   FETCH_CONSTANT                                   ~34     'CYCLES_ITERATION'
         53        ASSIGN_OBJ_OP                                 1          !3, 'iterations'
         54        OP_DATA                                                  ~34
  109    55        INIT_FCALL                                               'microtime'
         56        SEND_VAL                                                 <true>
         57        DO_ICALL                                         $36     
         58        SUB                                              ~37     $36, !4
         59        ASSIGN_OBJ_OP                                 1          !3, 'timing'
         60        OP_DATA                                                  ~37
  101    61      > JMP                                                      ->38
         62    >   FE_FREE                                                  $24
   96    63    >   INIT_FCALL                                               'microtime'
         64        SEND_VAL                                                 <true>
         65        DO_ICALL                                         $38     
         66        ASSIGN                                           ~39     !6, $38
         67        IS_SMALLER                                               ~39, !2
         68      > JMPNZ                                                    ~40, ->34
  113    69    >   INIT_STATIC_METHOD_CALL                                  'BenchmarkCase', 'printResults'
         70        SEND_VAR                                                 !1
         71        SEND_VAR                                                 !0
         72        DO_FCALL                                      0          
         73      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FVqt7o%3A36%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Vqt7o
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   37     2        FETCH_OBJ_R                                      ~2      !0, 'timing'
          3        FETCH_OBJ_R                                      ~3      !1, 'timing'
          4        SPACESHIP                                        ~4      ~2, ~3
          5      > RETURN                                                   ~4
   38     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FVqt7o%3A36%240

Function %00%7Bclosure%7D%2Fin%2FVqt7o%3A56%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/Vqt7o
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $severity, !1 = $message
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   57     2        NEW                                              $2      'ErrorException'
          3        SEND_VAR_EX                                              !1
          4        SEND_VAL_EX                                              0
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
          7      > THROW                                         0          $2
   58     8*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FVqt7o%3A56%241

Function %00%7Bclosure%7D%2Fin%2FVqt7o%3A61%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 3
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 3
Branch analysis from position: 13
Branch analysis from position: 3
filename:       /in/Vqt7o
function name:  {closure}
number of ops:  15
compiled vars:  !0 = $arr, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   62     0  E >   ASSIGN                                                   !0, <array>
   64     1        ASSIGN                                                   !1, 0
          2      > JMP                                                      ->11
   65     3    >   INIT_FCALL                                               'array_merge'
          4        SEND_VAR                                                 !0
          5        INIT_ARRAY                                       ~4      !1
          6        ADD_ARRAY_ELEMENT                                ~4      !1
          7        SEND_VAL                                                 ~4
          8        DO_ICALL                                         $5      
          9        ASSIGN                                                   !0, $5
   64    10        PRE_INC                                                  !1
         11    >   IS_SMALLER                                               !1, 100
         12      > JMPNZ                                                    ~8, ->3
   68    13    > > RETURN                                                   !0
   69    14*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FVqt7o%3A61%242

Function %00%7Bclosure%7D%2Fin%2FVqt7o%3A70%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 10, Position 2 = 3
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 10, Position 2 = 3
Branch analysis from position: 10
Branch analysis from position: 3
filename:       /in/Vqt7o
function name:  {closure}
number of ops:  16
compiled vars:  !0 = $arrArr, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   ASSIGN                                                   !0, <array>
   73     1        ASSIGN                                                   !1, 0
          2      > JMP                                                      ->8
   74     3    >   INIT_ARRAY                                       ~5      !1
          4        ADD_ARRAY_ELEMENT                                ~5      !1
          5        ASSIGN_DIM                                               !0
          6        OP_DATA                                                  ~5
   73     7        PRE_INC                                                  !1
          8    >   IS_SMALLER                                               !1, 100
          9      > JMPNZ                                                    ~7, ->3
   77    10    >   INIT_FCALL                                               'array_merge'
         11        SEND_UNPACK                                              !0
         12        CHECK_UNDEF_ARGS                                         
         13        DO_ICALL                                         $8      
         14      > RETURN                                                   $8
   78    15*     > RETURN                                                   null

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

Function %00%7Bclosure%7D%2Fin%2FVqt7o%3A79%244:
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 = 3
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 3
Branch analysis from position: 11
Branch analysis from position: 3
filename:       /in/Vqt7o
function name:  {closure}
number of ops:  13
compiled vars:  !0 = $arr, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   ASSIGN                                                   !0, <array>
   82     1        ASSIGN                                                   !1, 0
          2      > JMP                                                      ->9
   83     3    >   INIT_FCALL                                               'array_push'
          4        SEND_REF                                                 !0
          5        SEND_VAR                                                 !1
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                                 
   82     8        PRE_INC                                                  !1
          9    >   IS_SMALLER                                               !1, 100
         10      > JMPNZ                                                    ~6, ->3
   86    11    > > RETURN                                                   !0
   87    12*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FVqt7o%3A79%244

Class BenchmarkCase:
Function create:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Vqt7o
function name:  create
number of ops:  21
compiled vars:  !0 = $name, !1 = $callable, !2 = $self
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   21     2        NEW                          self                $3      
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !2, $3
   22     5        ASSIGN_OBJ                                               !2, 'name'
          6        OP_DATA                                                  !0
   23     7        ASSIGN_OBJ                                               !2, 'callable'
          8        OP_DATA                                                  !1
   25     9        INIT_FCALL                                               'max'
         10        FETCH_STATIC_PROP_R          unknown             ~9      'maxNameLength'
         11        SEND_VAL                                                 ~9
         12        STRLEN                                           ~10     !0
         13        SEND_VAL                                                 ~10
         14        DO_ICALL                                         $11     
         15        ASSIGN_STATIC_PROP                                       'maxNameLength'
         16        OP_DATA                                                  $11
   27    17        VERIFY_RETURN_TYPE                                       !2
         18      > RETURN                                                   !2
   28    19*       VERIFY_RETURN_TYPE                                       
         20*     > RETURN                                                   null

End of function create

Function printresults:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 19, Position 2 = 49
Branch analysis from position: 19
2 jumps found. (Code = 78) Position 1 = 20, Position 2 = 49
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Branch analysis from position: 49
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 49
filename:       /in/Vqt7o
function name:  printResults
number of ops:  55
compiled vars:  !0 = $initialMs, !1 = $cases, !2 = $durationMs, !3 = $caseFirst, !4 = $case
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   32     2        INIT_FCALL                                               'microtime'
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                         $5      
          5        SUB                                              ~6      $5, !0
          6        ASSIGN                                                   !2, ~6
   34     7        INIT_FCALL                                               'printf'
          8        SEND_VAL                                                 '%0D'
          9        DO_ICALL                                                 
   36    10        INIT_FCALL                                               'usort'
         11        SEND_REF                                                 !1
         12        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FVqt7o%3A36%240'
   38    13        SEND_VAL                                                 ~9
         14        DO_ICALL                                                 
   40    15        FETCH_DIM_R                                      ~11     !1, 0
         16        FETCH_OBJ_R                                      ~12     ~11, 'timing'
         17        ASSIGN                                                   !3, ~12
   43    18      > FE_RESET_R                                       $14     !1, ->49
         19    > > FE_FETCH_R                                               $14, !4, ->49
   44    20    >   INIT_FCALL                                               'printf'
         21        SEND_VAL                                                 'Case+%25s+duration+of+%25.6fs+%28avg.+each+%25.8fs%29+%2B%25.2f%25%25%0A'
   45    22        INIT_FCALL                                               'str_pad'
         23        FETCH_OBJ_R                                      ~15     !4, 'name'
         24        SEND_VAL                                                 ~15
         25        FETCH_STATIC_PROP_R          unknown             ~16     'maxNameLength'
         26        SEND_VAL                                                 ~16
         27        SEND_VAL                                                 '+'
         28        DO_ICALL                                         $17     
         29        SEND_VAR                                                 $17
   46    30        FETCH_OBJ_R                                      ~18     !4, 'timing'
         31        SEND_VAL                                                 ~18
   47    32        INIT_FCALL_BY_NAME                                       'bcdiv'
         33        FETCH_OBJ_R                                      ~19     !4, 'timing'
         34        CAST                                          6  ~20     ~19
         35        SEND_VAL_EX                                              ~20
         36        FETCH_OBJ_R                                      ~21     !4, 'iterations'
         37        CAST                                          6  ~22     ~21
         38        SEND_VAL_EX                                              ~22
         39        SEND_VAL_EX                                              8
         40        DO_FCALL                                      0  $23     
         41        SEND_VAR                                                 $23
   48    42        DIV                                              ~24     100, !3
         43        FETCH_OBJ_R                                      ~25     !4, 'timing'
         44        MUL                                              ~26     ~24, ~25
         45        SUB                                              ~27     ~26, 100
         46        SEND_VAL                                                 ~27
         47        DO_ICALL                                                 
   43    48      > JMP                                                      ->19
         49    >   FE_FREE                                                  $14
   51    50        INIT_FCALL                                               'printf'
         51        SEND_VAL                                                 '%0ATotal+duration%3A+%25.6fs'
   52    52        SEND_VAR                                                 !2
         53        DO_ICALL                                                 
   53    54      > RETURN                                                   null

End of function printresults

End of class BenchmarkCase.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.91 ms | 1424 KiB | 32 Q