3v4l.org

run code in 300+ PHP versions simultaneously
<?php $testList = [ [ 'array' => [], 'expect' => true, ], [ 'array' => ['a', 'b', 'c'], 'expect' => true, ], [ 'array' => ["0" => 'a', "1" => 'b', "2" => 'c'], 'expect' => true, ], [ 'array' => ["1" => 'a', "0" => 'b', "2" => 'c'], 'expect' => false, ], [ 'array' => ["a" => 'a', "b" => 'b', "c" => 'c'], 'expect' => false, ], ]; function displayBenchmark($function) { global $testList; echo '<h5>use function <code>' . $function . '</code></h5>'; $i = 0; $startTime = microtime(true); for ($i = 0; $i <= 1000; ++$i) { foreach ($testList as $testItem) { $result = $function($testItem['array']); assert($result === $testItem['expect'], 'Unexpect matched on index ' . $i); ++$i; } } $totalTime = microtime(true) - $startTime; echo 'total time: ' . var_export($totalTime, true) . '<br>'; } function array_is_list1(array $arr) { if ($arr === []) { return true; } return array_keys($arr) === range(0, count($arr) - 1); } function array_is_list2(array $array): bool { $i = -1; foreach ($array as $k => $v) { ++$i; if ($k !== $i) { return false; } } return true; } class Benchmark { private static $max, $memory; public static function memoryTick() { self::$memory = memory_get_usage() - self::$memory; self::$max = self::$memory>self::$max?self::$memory:self::$max; self::$memory = memory_get_usage(); } public static function benchmarkMemory(callable $function, $args=null) { declare(ticks=1); self::$memory = memory_get_usage(); self::$max = 0; register_tick_function('call_user_func_array', ['Benchmark', 'memoryTick'], []); $result = is_array($args)? call_user_func_array($function, $args): call_user_func($function); unregister_tick_function('call_user_func_array'); return [ 'memory' => self::$max ]; } } var_dump(Benchmark::benchmarkMemory('displayBenchmark', ['array_is_list'])); var_dump(Benchmark::benchmarkMemory('displayBenchmark', ['array_is_list1'])); var_dump(Benchmark::benchmarkMemory('displayBenchmark', ['array_is_list2']));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/paaK1
function name:  (null)
number of ops:  26
compiled vars:  !0 = $testList
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   93     1        INIT_FCALL                                               'var_dump'
          2        INIT_STATIC_METHOD_CALL                                  'Benchmark', 'benchmarkMemory'
          3        SEND_VAL                                                 'displayBenchmark'
          4        SEND_VAL                                                 <array>
          5        DO_FCALL                                      0  $2      
          6        SEND_VAR                                                 $2
          7        DO_ICALL                                                 
          8        TICKS                                                    
   94     9        INIT_FCALL                                               'var_dump'
         10        INIT_STATIC_METHOD_CALL                                  'Benchmark', 'benchmarkMemory'
         11        SEND_VAL                                                 'displayBenchmark'
         12        SEND_VAL                                                 <array>
         13        DO_FCALL                                      0  $4      
         14        SEND_VAR                                                 $4
         15        DO_ICALL                                                 
         16        TICKS                                                    
   95    17        INIT_FCALL                                               'var_dump'
         18        INIT_STATIC_METHOD_CALL                                  'Benchmark', 'benchmarkMemory'
         19        SEND_VAL                                                 'displayBenchmark'
         20        SEND_VAL                                                 <array>
         21        DO_FCALL                                      0  $6      
         22        SEND_VAR                                                 $6
         23        DO_ICALL                                                 
         24        TICKS                                                    
         25      > RETURN                                                   1

Function displaybenchmark:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
2 jumps found. (Code = 44) Position 1 = 34, Position 2 = 12
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 30
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 30
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 34, Position 2 = 12
Branch analysis from position: 34
Branch analysis from position: 12
Branch analysis from position: 30
filename:       /in/paaK1
function name:  displayBenchmark
number of ops:  47
compiled vars:  !0 = $function, !1 = $testList, !2 = $i, !3 = $startTime, !4 = $testItem, !5 = $result, !6 = $totalTime
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   29     1        BIND_GLOBAL                                              !1, 'testList'
   30     2        CONCAT                                           ~7      '%3Ch5%3Euse+function+%3Ccode%3E', !0
          3        CONCAT                                           ~8      ~7, '%3C%2Fcode%3E%3C%2Fh5%3E'
          4        ECHO                                                     ~8
   32     5        ASSIGN                                                   !2, 0
   33     6        INIT_FCALL                                               'microtime'
          7        SEND_VAL                                                 <true>
          8        DO_ICALL                                         $10     
          9        ASSIGN                                                   !3, $10
   34    10        ASSIGN                                                   !2, 0
         11      > JMP                                                      ->32
   35    12    > > FE_RESET_R                                       $13     !1, ->30
         13    > > FE_FETCH_R                                               $13, !4, ->30
   36    14    >   INIT_DYNAMIC_CALL                                        !0
         15        CHECK_FUNC_ARG                                           
         16        FETCH_DIM_FUNC_ARG                               $14     !4, 'array'
         17        SEND_FUNC_ARG                                            $14
         18        DO_FCALL                                      0  $15     
         19        ASSIGN                                                   !5, $15
   37    20        ASSERT_CHECK                                             
         21        INIT_FCALL                                               'assert'
         22        FETCH_DIM_R                                      ~17     !4, 'expect'
         23        IS_IDENTICAL                                     ~18     !5, ~17
         24        SEND_VAL                                                 ~18
         25        CONCAT                                           ~19     'Unexpect+matched+on+index+', !2
         26        SEND_VAL                                                 ~19
         27        DO_ICALL                                                 
   38    28        PRE_INC                                                  !2
   35    29      > JMP                                                      ->13
         30    >   FE_FREE                                                  $13
   34    31        PRE_INC                                                  !2
         32    >   IS_SMALLER_OR_EQUAL                                      !2, 1000
         33      > JMPNZ                                                    ~23, ->12
   41    34    >   INIT_FCALL                                               'microtime'
         35        SEND_VAL                                                 <true>
         36        DO_ICALL                                         $24     
         37        SUB                                              ~25     $24, !3
         38        ASSIGN                                                   !6, ~25
   42    39        INIT_FCALL                                               'var_export'
         40        SEND_VAR                                                 !6
         41        SEND_VAL                                                 <true>
         42        DO_ICALL                                         $27     
         43        CONCAT                                           ~28     'total+time%3A+', $27
         44        CONCAT                                           ~29     ~28, '%3Cbr%3E'
         45        ECHO                                                     ~29
   43    46      > RETURN                                                   null

End of function displaybenchmark

Function array_is_list1:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/paaK1
function name:  array_is_list1
number of ops:  16
compiled vars:  !0 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
   47     1        IS_IDENTICAL                                             !0, <array>
          2      > JMPZ                                                     ~1, ->4
   48     3    > > RETURN                                                   <true>
   50     4    >   INIT_FCALL                                               'array_keys'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $2      
          7        INIT_FCALL                                               'range'
          8        SEND_VAL                                                 0
          9        COUNT                                            ~3      !0
         10        SUB                                              ~4      ~3, 1
         11        SEND_VAL                                                 ~4
         12        DO_ICALL                                         $5      
         13        IS_IDENTICAL                                     ~6      $2, $5
         14      > RETURN                                                   ~6
   51    15*     > RETURN                                                   null

End of function array_is_list1

Function array_is_list2:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 11
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/paaK1
function name:  array_is_list2
number of ops:  15
compiled vars:  !0 = $array, !1 = $i, !2 = $v, !3 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   53     0  E >   RECV                                             !0      
   55     1        ASSIGN                                                   !1, -1
   56     2      > FE_RESET_R                                       $5      !0, ->11
          3    > > FE_FETCH_R                                       ~6      $5, !2, ->11
          4    >   ASSIGN                                                   !3, ~6
   57     5        PRE_INC                                                  !1
   58     6        IS_NOT_IDENTICAL                                         !3, !1
          7      > JMPZ                                                     ~9, ->10
   59     8    >   FE_FREE                                                  $5
          9      > RETURN                                                   <false>
   56    10    > > JMP                                                      ->3
         11    >   FE_FREE                                                  $5
   62    12      > RETURN                                                   <true>
   63    13*       VERIFY_RETURN_TYPE                                       
         14*     > RETURN                                                   null

End of function array_is_list2

Class Benchmark:
Function memorytick:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 13
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/paaK1
function name:  memoryTick
number of ops:  22
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   INIT_FCALL                                               'memory_get_usage'
          1        DO_ICALL                                         $1      
          2        FETCH_STATIC_PROP_R          unknown             ~2      'memory'
          3        SUB                                              ~3      $1, ~2
          4        ASSIGN_STATIC_PROP                                       'memory'
          5        OP_DATA                                                  ~3
   72     6        FETCH_STATIC_PROP_R          unknown             ~5      'memory'
          7        FETCH_STATIC_PROP_R          unknown             ~6      'max'
          8        IS_SMALLER                                               ~6, ~5
          9      > JMPZ                                                     ~7, ->13
         10    >   FETCH_STATIC_PROP_R          unknown             ~8      'memory'
         11        QM_ASSIGN                                        ~9      ~8
         12      > JMP                                                      ->15
         13    >   FETCH_STATIC_PROP_R          unknown             ~10     'max'
         14        QM_ASSIGN                                        ~9      ~10
         15    >   ASSIGN_STATIC_PROP                                       'max'
         16        OP_DATA                                                  ~9
   73    17        INIT_FCALL                                               'memory_get_usage'
         18        DO_ICALL                                         $12     
         19        ASSIGN_STATIC_PROP                                       'memory'
         20        OP_DATA                                                  $12
   74    21      > RETURN                                                   null

End of function memorytick

Function benchmarkmemory:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 25
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/paaK1
function name:  benchmarkMemory
number of ops:  39
compiled vars:  !0 = $function, !1 = $args, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   76     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
   78     2        TICKS                                                    
   79     3        INIT_FCALL                                               'memory_get_usage'
          4        DO_ICALL                                         $4      
          5        ASSIGN_STATIC_PROP                                       'memory'
          6        OP_DATA                                                  $4
          7        TICKS                                                    
   80     8        ASSIGN_STATIC_PROP                                       'max'
          9        OP_DATA                                                  0
         10        TICKS                                                    
   82    11        INIT_FCALL                                               'register_tick_function'
         12        SEND_VAL                                                 'call_user_func_array'
         13        SEND_VAL                                                 <array>
         14        SEND_VAL                                                 <array>
         15        DO_ICALL                                                 
         16        TICKS                                                    
   83    17        TYPE_CHECK                                  128          !1
         18      > JMPZ                                                     ~7, ->25
   84    19    >   INIT_USER_CALL                                0          'call_user_func_array', !0
         20        SEND_ARRAY                                               !1
         21        CHECK_UNDEF_ARGS                                         
         22        DO_FCALL                                      1  $8      
         23        QM_ASSIGN                                        ~9      $8
         24      > JMP                                                      ->28
   85    25    >   INIT_USER_CALL                                0          'call_user_func', !0
         26        DO_FCALL                                      0  $10     
         27        QM_ASSIGN                                        ~9      $10
   83    28    >   ASSIGN                                                   !2, ~9
         29        TICKS                                                    
   86    30        INIT_FCALL                                               'unregister_tick_function'
         31        SEND_VAL                                                 'call_user_func_array'
         32        DO_ICALL                                                 
         33        TICKS                                                    
   88    34        FETCH_STATIC_PROP_R          unknown             ~13     'max'
         35        INIT_ARRAY                                       ~14     ~13, 'memory'
         36      > RETURN                                                   ~14
         37*       TICKS                                                    
   90    38*     > RETURN                                                   null

End of function benchmarkmemory

End of class Benchmark.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.35 ms | 1452 KiB | 22 Q