3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @param array $functions An associative array of closures to benchmark * @param int $iterations The number of iterations */ function benchmark($functions, $iterations) { foreach ($functions as $name => $function) { $start = microtime(true); for ($i = 0; $i < $iterations; $i++) { call_user_func($function); } $delta = microtime(true) - $start; echo "$name: " . $delta . "\n"; } } echo "\nBenchmarking \"(array) \$var === \$var\" vs \"is_array(\$var)\" ...\n"; $data = array( 'string', // Not an array array(1, 2, 3), // Small array array_fill(1, 1000, uniqid()) // Large-ish array ); benchmark(array( 'casting' => function () use ($data) { foreach ($data as $d) { $result = (array) $d === $d; } }, 'is_array' => function () use ($data) { foreach ($data as $d) { $result = is_array($d); } } ), 1000); echo "\nBenchmarking \"count(\$var) == 0\" vs \"empty(\$var)\" ...\n"; $data = range(1, 100000); benchmark(array( 'count()' => function () use ($data) { $result = count($data) == 0; }, 'empty()' => function () use ($data) { $result = empty($data); } ), 100000); echo "\nBenchmarking \"array_key_exists(\$key, \$array)\" vs \"\$array[\$key]\" ...\n"; $data = array_fill_keys(range(1, 1000), uniqid()); benchmark(array( 'in_array_keys()' => function () use ($data) { $result = array_key_exists(50, $data) ? : null; }, 'key check' => function () use ($data) { $result = isset($data[50]) ? $data[50] : null; } ), 100000);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2qkS3
function name:  (null)
number of ops:  61
compiled vars:  !0 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   ECHO                                                     '%0ABenchmarking+%22%28array%29+%24var+%3D%3D%3D+%24var%22+vs+%22is_array%28%24var%29%22+...%0A'
   26     1        INIT_ARRAY                                       ~1      'string'
          2        ADD_ARRAY_ELEMENT                                ~1      <array>
   28     3        INIT_FCALL                                               'array_fill'
          4        SEND_VAL                                                 1
          5        SEND_VAL                                                 1000
          6        INIT_FCALL                                               'uniqid'
          7        DO_ICALL                                         $2      
          8        SEND_VAR                                                 $2
          9        DO_ICALL                                         $3      
         10        ADD_ARRAY_ELEMENT                                ~1      $3
   25    11        ASSIGN                                                   !0, ~1
   31    12        INIT_FCALL                                               'benchmark'
   32    13        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F2qkS3%3A32%240'
         14        BIND_LEXICAL                                             ~5, !0
   36    15        INIT_ARRAY                                       ~6      ~5, 'casting'
   37    16        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F2qkS3%3A37%241'
         17        BIND_LEXICAL                                             ~7, !0
   41    18        ADD_ARRAY_ELEMENT                                ~6      ~7, 'is_array'
         19        SEND_VAL                                                 ~6
   42    20        SEND_VAL                                                 1000
         21        DO_FCALL                                      0          
   45    22        ECHO                                                     '%0ABenchmarking+%22count%28%24var%29+%3D%3D+0%22+vs+%22empty%28%24var%29%22+...%0A'
   47    23        INIT_FCALL                                               'range'
         24        SEND_VAL                                                 1
         25        SEND_VAL                                                 100000
         26        DO_ICALL                                         $9      
         27        ASSIGN                                                   !0, $9
   49    28        INIT_FCALL                                               'benchmark'
   50    29        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F2qkS3%3A50%242'
         30        BIND_LEXICAL                                             ~11, !0
   52    31        INIT_ARRAY                                       ~12     ~11, 'count%28%29'
   53    32        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F2qkS3%3A53%243'
         33        BIND_LEXICAL                                             ~13, !0
   55    34        ADD_ARRAY_ELEMENT                                ~12     ~13, 'empty%28%29'
         35        SEND_VAL                                                 ~12
   56    36        SEND_VAL                                                 100000
         37        DO_FCALL                                      0          
   59    38        ECHO                                                     '%0ABenchmarking+%22array_key_exists%28%24key%2C+%24array%29%22+vs+%22%24array%5B%24key%5D%22+...%0A'
   61    39        INIT_FCALL                                               'array_fill_keys'
         40        INIT_FCALL                                               'range'
         41        SEND_VAL                                                 1
         42        SEND_VAL                                                 1000
         43        DO_ICALL                                         $15     
         44        SEND_VAR                                                 $15
         45        INIT_FCALL                                               'uniqid'
         46        DO_ICALL                                         $16     
         47        SEND_VAR                                                 $16
         48        DO_ICALL                                         $17     
         49        ASSIGN                                                   !0, $17
   63    50        INIT_FCALL                                               'benchmark'
   64    51        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F2qkS3%3A64%244'
         52        BIND_LEXICAL                                             ~19, !0
   66    53        INIT_ARRAY                                       ~20     ~19, 'in_array_keys%28%29'
   67    54        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F2qkS3%3A67%245'
         55        BIND_LEXICAL                                             ~21, !0
   69    56        ADD_ARRAY_ELEMENT                                ~20     ~21, 'key+check'
         57        SEND_VAL                                                 ~20
   70    58        SEND_VAL                                                 100000
         59        DO_FCALL                                      0          
         60      > RETURN                                                   1

Function benchmark:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 27
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 27
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 11
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 11
Branch analysis from position: 16
Branch analysis from position: 11
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
filename:       /in/2qkS3
function name:  benchmark
number of ops:  29
compiled vars:  !0 = $functions, !1 = $iterations, !2 = $function, !3 = $name, !4 = $start, !5 = $i, !6 = $delta
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    9     2      > FE_RESET_R                                       $7      !0, ->27
          3    > > FE_FETCH_R                                       ~8      $7, !2, ->27
          4    >   ASSIGN                                                   !3, ~8
   10     5        INIT_FCALL                                               'microtime'
          6        SEND_VAL                                                 <true>
          7        DO_ICALL                                         $10     
          8        ASSIGN                                                   !4, $10
   12     9        ASSIGN                                                   !5, 0
         10      > JMP                                                      ->14
   13    11    >   INIT_USER_CALL                                0          'call_user_func', !2
         12        DO_FCALL                                      0          
   12    13        PRE_INC                                                  !5
         14    >   IS_SMALLER                                               !5, !1
         15      > JMPNZ                                                    ~15, ->11
   16    16    >   INIT_FCALL                                               'microtime'
         17        SEND_VAL                                                 <true>
         18        DO_ICALL                                         $16     
         19        SUB                                              ~17     $16, !4
         20        ASSIGN                                                   !6, ~17
   18    21        NOP                                                      
         22        FAST_CONCAT                                      ~19     !3, '%3A+'
         23        CONCAT                                           ~20     ~19, !6
         24        CONCAT                                           ~21     ~20, '%0A'
         25        ECHO                                                     ~21
    9    26      > JMP                                                      ->3
         27    >   FE_FREE                                                  $7
   20    28      > RETURN                                                   null

End of function benchmark

Function %00%7Bclosure%7D%2Fin%2F2qkS3%3A32%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 7
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/2qkS3
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $data, !1 = $d, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   BIND_STATIC                                              !0
   33     1      > FE_RESET_R                                       $3      !0, ->7
          2    > > FE_FETCH_R                                               $3, !1, ->7
   34     3    >   CAST                                          7  ~4      !1
          4        IS_IDENTICAL                                     ~5      !1, ~4
          5        ASSIGN                                                   !2, ~5
   33     6      > JMP                                                      ->2
          7    >   FE_FREE                                                  $3
   36     8      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F2qkS3%3A32%240

Function %00%7Bclosure%7D%2Fin%2F2qkS3%3A37%241:
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/2qkS3
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $data, !1 = $d, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   BIND_STATIC                                              !0
   38     1      > FE_RESET_R                                       $3      !0, ->6
          2    > > FE_FETCH_R                                               $3, !1, ->6
   39     3    >   TYPE_CHECK                                  128  ~4      !1
          4        ASSIGN                                                   !2, ~4
   38     5      > JMP                                                      ->2
          6    >   FE_FREE                                                  $3
   41     7      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F2qkS3%3A37%241

Function %00%7Bclosure%7D%2Fin%2F2qkS3%3A50%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2qkS3
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $data, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   BIND_STATIC                                              !0
   51     1        COUNT                                            ~2      !0
          2        IS_EQUAL                                         ~3      ~2, 0
          3        ASSIGN                                                   !1, ~3
   52     4      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F2qkS3%3A50%242

Function %00%7Bclosure%7D%2Fin%2F2qkS3%3A53%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2qkS3
function name:  {closure}
number of ops:  4
compiled vars:  !0 = $data, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   53     0  E >   BIND_STATIC                                              !0
   54     1        ISSET_ISEMPTY_CV                                 ~2      !0
          2        ASSIGN                                                   !1, ~2
   55     3      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F2qkS3%3A53%243

Function %00%7Bclosure%7D%2Fin%2F2qkS3%3A64%244:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2qkS3
function name:  {closure}
number of ops:  6
compiled vars:  !0 = $data, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   BIND_STATIC                                              !0
   65     1        ARRAY_KEY_EXISTS                                 ~2      50, !0
          2        JMP_SET                                          ~3      ~2, ->4
          3        QM_ASSIGN                                        ~3      null
          4        ASSIGN                                                   !1, ~3
   66     5      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F2qkS3%3A64%244

Function %00%7Bclosure%7D%2Fin%2F2qkS3%3A67%245:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2qkS3
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $data, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   BIND_STATIC                                              !0
   68     1        ISSET_ISEMPTY_DIM_OBJ                         0          !0, 50
          2      > JMPZ                                                     ~2, ->6
          3    >   FETCH_DIM_R                                      ~3      !0, 50
          4        QM_ASSIGN                                        ~4      ~3
          5      > JMP                                                      ->7
          6    >   QM_ASSIGN                                        ~4      null
          7    >   ASSIGN                                                   !1, ~4
   69     8      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F2qkS3%3A67%245

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.45 ms | 1406 KiB | 26 Q