3v4l.org

run code in 300+ PHP versions simultaneously
<?php function benchmark(callable ...$functions): void { $times = array_combine(array_keys($functions), array_fill(0, count($functions), 0.0)); $baseTime = 0.0; $baseCallable = fn() => null; $start = hrtime(true); $count = 0; while (hrtime(true) < $start + 1e9) { $startCall = hrtime(true); $baseCallable(); $baseTime += hrtime(true) - $startCall; foreach ($functions as $key => $function) { $startCall = hrtime(true); $function(); $times[$key] += hrtime(true) - $startCall; } $count++; } foreach ($times as $key => $time) { $times[$key] = $time - $baseTime; } if ($count < 100) { echo "Methods too low. Iterated only {$count} times\n"; } asort($times); echo "Benchmark results after {$count} iterations in nanoseconds:\n"; foreach ($times as $key => $time) { echo $key . ': ' . $time . "\n"; } } enum OptionEnum: string { case Option1 = 'opt1'; case Option2 = 'opt2'; case Option3 = 'opt3'; case Option4 = 'opt4'; case Option5 = 'opt5'; public static function values(): array { return ['opt1', 'opt2', 'opt3', 'opt4', 'opt5']; } public static function valuesArrayMap(): array { return array_map(static fn(self $c) => $c->value, self::cases()); } public static function valuesArrayColumn(): array { return array_column(self::cases(), 'value'); } } benchmark( arrayRaw: fn() => OptionEnum::values(), arrayMap: fn() => OptionEnum::valuesArrayMap(), arrayCol: fn() => OptionEnum::valuesArrayColumn(), );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q5AYg
function name:  (null)
number of ops:  11
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   DECLARE_CLASS                                            'optionenum'
   64     1        INIT_FCALL                                               'benchmark'
   65     2        DECLARE_LAMBDA_FUNCTION                          ~0      [0]
          3        SEND_VAL_EX                                              ~0, 'arrayRaw'
   66     4        DECLARE_LAMBDA_FUNCTION                          ~1      [1]
          5        SEND_VAL_EX                                              ~1, 'arrayMap'
   67     6        DECLARE_LAMBDA_FUNCTION                          ~2      [2]
          7        SEND_VAL_EX                                              ~2, 'arrayCol'
          8        CHECK_UNDEF_ARGS                                         
   64     9        DO_FCALL                                      1          
   68    10      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q5AYg
function name:  {closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   INIT_STATIC_METHOD_CALL                                  'OptionEnum', 'values'
          1        DO_FCALL                                      0  $0      
          2      > RETURN                                                   $0
          3*     > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q5AYg
function name:  {closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     0  E >   INIT_STATIC_METHOD_CALL                                  'OptionEnum', 'valuesArrayMap'
          1        DO_FCALL                                      0  $0      
          2      > RETURN                                                   $0
          3*     > RETURN                                                   null

End of Dynamic Function 1

Dynamic Function 2
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q5AYg
function name:  {closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   INIT_STATIC_METHOD_CALL                                  'OptionEnum', 'valuesArrayColumn'
          1        DO_FCALL                                      0  $0      
          2      > RETURN                                                   $0
          3*     > RETURN                                                   null

End of Dynamic Function 2

Function benchmark:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 53
Branch analysis from position: 53
2 jumps found. (Code = 44) Position 1 = 59, Position 2 = 24
Branch analysis from position: 59
2 jumps found. (Code = 77) Position 1 = 60, Position 2 = 66
Branch analysis from position: 60
2 jumps found. (Code = 78) Position 1 = 61, Position 2 = 66
Branch analysis from position: 61
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
Branch analysis from position: 66
2 jumps found. (Code = 43) Position 1 = 69, Position 2 = 73
Branch analysis from position: 69
2 jumps found. (Code = 77) Position 1 = 81, Position 2 = 88
Branch analysis from position: 81
2 jumps found. (Code = 78) Position 1 = 82, Position 2 = 88
Branch analysis from position: 82
1 jumps found. (Code = 42) Position 1 = 81
Branch analysis from position: 81
Branch analysis from position: 88
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 88
Branch analysis from position: 73
Branch analysis from position: 66
Branch analysis from position: 24
2 jumps found. (Code = 77) Position 1 = 36, Position 2 = 51
Branch analysis from position: 36
2 jumps found. (Code = 78) Position 1 = 37, Position 2 = 51
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
Branch analysis from position: 51
2 jumps found. (Code = 44) Position 1 = 59, Position 2 = 24
Branch analysis from position: 59
Branch analysis from position: 24
Branch analysis from position: 51
filename:       /in/Q5AYg
function name:  benchmark
number of ops:  90
compiled vars:  !0 = $functions, !1 = $times, !2 = $baseTime, !3 = $baseCallable, !4 = $start, !5 = $count, !6 = $startCall, !7 = $function, !8 = $key, !9 = $time
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV_VARIADIC                                    !0      
    5     1        INIT_FCALL                                               'array_combine'
          2        INIT_FCALL                                               'array_keys'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $10     
          5        SEND_VAR                                                 $10
          6        INIT_FCALL                                               'array_fill'
          7        SEND_VAL                                                 0
          8        COUNT                                            ~11     !0
          9        SEND_VAL                                                 ~11
         10        SEND_VAL                                                 0
         11        DO_ICALL                                         $12     
         12        SEND_VAR                                                 $12
         13        DO_ICALL                                         $13     
         14        ASSIGN                                                   !1, $13
    6    15        ASSIGN                                                   !2, 0
    7    16        DECLARE_LAMBDA_FUNCTION                          ~16     [0]
         17        ASSIGN                                                   !3, ~16
    8    18        INIT_FCALL                                               'hrtime'
         19        SEND_VAL                                                 <true>
         20        DO_ICALL                                         $18     
         21        ASSIGN                                                   !4, $18
    9    22        ASSIGN                                                   !5, 0
   11    23      > JMP                                                      ->53
   12    24    >   INIT_FCALL                                               'hrtime'
         25        SEND_VAL                                                 <true>
         26        DO_ICALL                                         $21     
         27        ASSIGN                                                   !6, $21
   13    28        INIT_DYNAMIC_CALL                                        !3
         29        DO_FCALL                                      0          
   14    30        INIT_FCALL                                               'hrtime'
         31        SEND_VAL                                                 <true>
         32        DO_ICALL                                         $24     
         33        SUB                                              ~25     $24, !6
         34        ASSIGN_OP                                     1          !2, ~25
   16    35      > FE_RESET_R                                       $27     !0, ->51
         36    > > FE_FETCH_R                                       ~28     $27, !7, ->51
         37    >   ASSIGN                                                   !8, ~28
   17    38        INIT_FCALL                                               'hrtime'
         39        SEND_VAL                                                 <true>
         40        DO_ICALL                                         $30     
         41        ASSIGN                                                   !6, $30
   18    42        INIT_DYNAMIC_CALL                                        !7
         43        DO_FCALL                                      0          
   19    44        INIT_FCALL                                               'hrtime'
         45        SEND_VAL                                                 <true>
         46        DO_ICALL                                         $34     
         47        SUB                                              ~35     $34, !6
         48        ASSIGN_DIM_OP                +=               1          !1, !8
         49        OP_DATA                                                  ~35
   16    50      > JMP                                                      ->36
         51    >   FE_FREE                                                  $27
   21    52        PRE_INC                                                  !5
   11    53    >   INIT_FCALL                                               'hrtime'
         54        SEND_VAL                                                 <true>
         55        DO_ICALL                                         $37     
         56        ADD                                              ~38     !4, 1.0e+9
         57        IS_SMALLER                                               $37, ~38
         58      > JMPNZ                                                    ~39, ->24
   24    59    > > FE_RESET_R                                       $40     !1, ->66
         60    > > FE_FETCH_R                                       ~41     $40, !9, ->66
         61    >   ASSIGN                                                   !8, ~41
   25    62        SUB                                              ~44     !9, !2
         63        ASSIGN_DIM                                               !1, !8
         64        OP_DATA                                                  ~44
   24    65      > JMP                                                      ->60
         66    >   FE_FREE                                                  $40
   28    67        IS_SMALLER                                               !5, 100
         68      > JMPZ                                                     ~45, ->73
   29    69    >   ROPE_INIT                                     3  ~47     'Methods+too+low.+Iterated+only+'
         70        ROPE_ADD                                      1  ~47     ~47, !5
         71        ROPE_END                                      2  ~46     ~47, '+times%0A'
         72        ECHO                                                     ~46
   32    73    >   INIT_FCALL                                               'asort'
         74        SEND_REF                                                 !1
         75        DO_ICALL                                                 
   34    76        ROPE_INIT                                     3  ~51     'Benchmark+results+after+'
         77        ROPE_ADD                                      1  ~51     ~51, !5
         78        ROPE_END                                      2  ~50     ~51, '+iterations+in+nanoseconds%3A%0A'
         79        ECHO                                                     ~50
   35    80      > FE_RESET_R                                       $53     !1, ->88
         81    > > FE_FETCH_R                                       ~54     $53, !9, ->88
         82    >   ASSIGN                                                   !8, ~54
   36    83        CONCAT                                           ~56     !8, '%3A+'
         84        CONCAT                                           ~57     ~56, !9
         85        CONCAT                                           ~58     ~57, '%0A'
         86        ECHO                                                     ~58
   35    87      > JMP                                                      ->81
         88    >   FE_FREE                                                  $53
   38    89      > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q5AYg
function name:  {closure}
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E > > RETURN                                                   null
          1*     > RETURN                                                   null

End of Dynamic Function 0

End of function benchmark

Class OptionEnum:
Function values:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q5AYg
function name:  values
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E > > RETURN                                                   <array>
   51     1*       VERIFY_RETURN_TYPE                                       
          2*     > RETURN                                                   null

End of function values

Function valuesarraymap:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q5AYg
function name:  valuesArrayMap
number of ops:  11
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   INIT_FCALL                                               'array_map'
          1        DECLARE_LAMBDA_FUNCTION                          ~0      [0]
          2        SEND_VAL                                                 ~0
          3        INIT_STATIC_METHOD_CALL                                  'cases'
          4        DO_FCALL                                      0  $1      
          5        SEND_VAR                                                 $1
          6        DO_ICALL                                         $2      
          7        VERIFY_RETURN_TYPE                                       $2
          8      > RETURN                                                   $2
   56     9*       VERIFY_RETURN_TYPE                                       
         10*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q5AYg
function name:  {closure}
number of ops:  4
compiled vars:  !0 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   RECV                                             !0      
          1        FETCH_OBJ_R                                      ~1      !0, 'value'
          2      > RETURN                                                   ~1
          3*     > RETURN                                                   null

End of Dynamic Function 0

End of function valuesarraymap

Function valuesarraycolumn:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q5AYg
function name:  valuesArrayColumn
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   INIT_FCALL                                               'array_column'
          1        INIT_STATIC_METHOD_CALL                                  'cases'
          2        DO_FCALL                                      0  $0      
          3        SEND_VAR                                                 $0
          4        SEND_VAL                                                 'value'
          5        DO_ICALL                                         $1      
          6        VERIFY_RETURN_TYPE                                       $1
          7      > RETURN                                                   $1
   61     8*       VERIFY_RETURN_TYPE                                       
          9*     > RETURN                                                   null

End of function valuesarraycolumn

End of class OptionEnum.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.99 ms | 1098 KiB | 21 Q