3v4l.org

run code in 300+ PHP versions simultaneously
<?php // array_values function method_1(Array &$arr) { return $arr === array_values($arr); } // method_2 was DQ; did not actually work // array_keys function method_3(Array &$arr) { return array_keys($arr) === range(0, count($arr) - 1); } // foreach function method_4(Array &$arr) { $idx = 0; foreach( $arr as $key => $val ){ if( $key !== $idx ) return FALSE; ++$idx; } return TRUE; } // guessing function method_5(Array &$arr) { global $METHOD_5_KEY; $i = 0; $l = count($arr)-1; end($arr); if ( key($arr) !== $l ) return FALSE; reset($arr); do { if ( $i !== key($arr) ) return FALSE; ++$i; next($arr); } while ($i < $l); return TRUE; } // naieve function method_6(Array &$arr) { $i = 0; $l = count($arr); do { if ( NULL === @$arr[$i] ) return FALSE; ++$i; } while ($i < $l); return TRUE; } // deep reference reliance function method_7(Array &$arr) { return array_keys(array_values($arr)) === array_keys($arr); } // organic (guessing + array_values) function method_8(Array &$arr) { reset($arr); if ( key($arr) !== 0 ) return FALSE; end($arr); if ( key($arr) !== count($arr)-1 ) return FALSE; return array_values($arr) === $arr; } function benchmark(Array &$methods, Array &$target, $expected){ foreach($methods as $method){ $start = microtime(true); for ($i = 0; $i < 2000; ++$i) { //$dummy = call_user_func($method, $target); if ( $method($target) !== $expected ) { echo "Method $method is disqualified for returning an incorrect result.\n"; unset($methods[array_search($method,$methods,true)]); $i = 0; break; } } if ( $i != 0 ) { $end = microtime(true); echo "Time taken with $method = ".round(($end-$start)*1000.0,3)."ms\n"; } } } $true_targets = [ 'Giant array' => range(0, 500), 'Tiny array' => range(0, 20), ]; $g = range(0,10); unset($g[0]); $false_targets = [ 'Large array 1' => range(0, 100) + ['a'=>'a'] + range(101, 200), 'Large array 2' => ['a'=>'a'] + range(0, 200), 'Tiny array' => range(0, 10) + ['a'=>'a'] + range(11, 20), 'Gotcha array' => $g, ]; $methods = [ 'method_1', 'method_3', 'method_4', 'method_5', 'method_6', 'method_7', 'method_8' ]; foreach($false_targets as $targetName => $target){ echo "==== Benchmark using $targetName expecing FALSE ====\n"; benchmark($methods, $target, false); echo "\n"; } foreach($true_targets as $targetName => $target){ echo "==== Benchmark using $targetName expecting TRUE ====\n"; benchmark($methods, $target, true); echo "\n"; }

Abusive script

This script was stopped while abusing our resources

Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 49, Position 2 = 62
Branch analysis from position: 49
2 jumps found. (Code = 78) Position 1 = 50, Position 2 = 62
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
Branch analysis from position: 62
2 jumps found. (Code = 77) Position 1 = 64, Position 2 = 77
Branch analysis from position: 64
2 jumps found. (Code = 78) Position 1 = 65, Position 2 = 77
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 64
Branch analysis from position: 64
Branch analysis from position: 77
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 77
Branch analysis from position: 62
filename:       /in/rkieX
function name:  (null)
number of ops:  79
compiled vars:  !0 = $true_targets, !1 = $g, !2 = $false_targets, !3 = $methods, !4 = $target, !5 = $targetName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   99     0  E >   INIT_FCALL                                               'range'
          1        SEND_VAL                                                 0
          2        SEND_VAL                                                 500
          3        DO_ICALL                                         $6      
          4        INIT_ARRAY                                       ~7      $6, 'Giant+array'
  100     5        INIT_FCALL                                               'range'
          6        SEND_VAL                                                 0
          7        SEND_VAL                                                 20
          8        DO_ICALL                                         $8      
          9        ADD_ARRAY_ELEMENT                                ~7      $8, 'Tiny+array'
   98    10        ASSIGN                                                   !0, ~7
  104    11        INIT_FCALL                                               'range'
         12        SEND_VAL                                                 0
         13        SEND_VAL                                                 10
         14        DO_ICALL                                         $10     
         15        ASSIGN                                                   !1, $10
  105    16        UNSET_DIM                                                !1, 0
  108    17        INIT_FCALL                                               'range'
         18        SEND_VAL                                                 0
         19        SEND_VAL                                                 100
         20        DO_ICALL                                         $12     
         21        ADD                                              ~13     $12, <array>
         22        INIT_FCALL                                               'range'
         23        SEND_VAL                                                 101
         24        SEND_VAL                                                 200
         25        DO_ICALL                                         $14     
         26        ADD                                              ~15     ~13, $14
         27        INIT_ARRAY                                       ~16     ~15, 'Large+array+1'
  109    28        INIT_FCALL                                               'range'
         29        SEND_VAL                                                 0
         30        SEND_VAL                                                 200
         31        DO_ICALL                                         $17     
         32        ADD                                              ~18     <array>, $17
         33        ADD_ARRAY_ELEMENT                                ~16     ~18, 'Large+array+2'
  110    34        INIT_FCALL                                               'range'
         35        SEND_VAL                                                 0
         36        SEND_VAL                                                 10
         37        DO_ICALL                                         $19     
  108    38        ADD                                              ~20     $19, <array>
  110    39        INIT_FCALL                                               'range'
         40        SEND_VAL                                                 11
         41        SEND_VAL                                                 20
         42        DO_ICALL                                         $21     
         43        ADD                                              ~22     ~20, $21
         44        ADD_ARRAY_ELEMENT                                ~16     ~22, 'Tiny+array'
  111    45        ADD_ARRAY_ELEMENT                                ~16     !1, 'Gotcha+array'
  107    46        ASSIGN                                                   !2, ~16
  114    47        ASSIGN                                                   !3, <array>
  125    48      > FE_RESET_R                                       $25     !2, ->62
         49    > > FE_FETCH_R                                       ~26     $25, !4, ->62
         50    >   ASSIGN                                                   !5, ~26
  126    51        ROPE_INIT                                     3  ~29     '%3D%3D%3D%3D+Benchmark+using+'
         52        ROPE_ADD                                      1  ~29     ~29, !5
         53        ROPE_END                                      2  ~28     ~29, '+expecing+FALSE+%3D%3D%3D%3D%0A'
         54        ECHO                                                     ~28
  127    55        INIT_FCALL                                               'benchmark'
         56        SEND_REF                                                 !3
         57        SEND_REF                                                 !4
         58        SEND_VAL                                                 <false>
         59        DO_FCALL                                      0          
  128    60        ECHO                                                     '%0A'
  125    61      > JMP                                                      ->49
         62    >   FE_FREE                                                  $25
  130    63      > FE_RESET_R                                       $32     !0, ->77
         64    > > FE_FETCH_R                                       ~33     $32, !4, ->77
         65    >   ASSIGN                                                   !5, ~33
  131    66        ROPE_INIT                                     3  ~36     '%3D%3D%3D%3D+Benchmark+using+'
         67        ROPE_ADD                                      1  ~36     ~36, !5
         68        ROPE_END                                      2  ~35     ~36, '+expecting+TRUE+%3D%3D%3D%3D%0A'
         69        ECHO                                                     ~35
  132    70        INIT_FCALL                                               'benchmark'
         71        SEND_REF                                                 !3
         72        SEND_REF                                                 !4
         73        SEND_VAL                                                 <true>
         74        DO_FCALL                                      0          
  133    75        ECHO                                                     '%0A'
  130    76      > JMP                                                      ->64
         77    >   FE_FREE                                                  $32
  134    78      > RETURN                                                   1

Function method_1:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rkieX
function name:  method_1
number of ops:  7
compiled vars:  !0 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
    5     1        INIT_FCALL                                               'array_values'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $1      
          4        IS_IDENTICAL                                     ~2      !0, $1
          5      > RETURN                                                   ~2
    6     6*     > RETURN                                                   null

End of function method_1

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

End of function method_3

Function method_4:
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 = 7, Position 2 = 9
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
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/rkieX
function name:  method_4
number of ops:  14
compiled vars:  !0 = $arr, !1 = $idx, !2 = $val, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
   17     1        ASSIGN                                                   !1, 0
   18     2      > FE_RESET_R                                       $5      !0, ->11
          3    > > FE_FETCH_R                                       ~6      $5, !2, ->11
          4    >   ASSIGN                                                   !3, ~6
   19     5        IS_NOT_IDENTICAL                                         !3, !1
          6      > JMPZ                                                     ~8, ->9
   20     7    >   FE_FREE                                                  $5
          8      > RETURN                                                   <false>
   21     9    >   PRE_INC                                                  !1
   18    10      > JMP                                                      ->3
         11    >   FE_FREE                                                  $5
   23    12      > RETURN                                                   <true>
   24    13*     > RETURN                                                   null

End of function method_4

Function method_5:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 23, Position 2 = 24
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 18
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/rkieX
function name:  method_5
number of ops:  32
compiled vars:  !0 = $arr, !1 = $METHOD_5_KEY, !2 = $i, !3 = $l
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   28     1        BIND_GLOBAL                                              !1, 'METHOD_5_KEY'
   29     2        ASSIGN                                                   !2, 0
   30     3        COUNT                                            ~5      !0
          4        SUB                                              ~6      ~5, 1
          5        ASSIGN                                                   !3, ~6
   32     6        INIT_FCALL                                               'end'
          7        SEND_REF                                                 !0
          8        DO_ICALL                                                 
   33     9        INIT_FCALL                                               'key'
         10        SEND_VAR                                                 !0
         11        DO_ICALL                                         $9      
         12        IS_NOT_IDENTICAL                                         !3, $9
         13      > JMPZ                                                     ~10, ->15
   34    14    > > RETURN                                                   <false>
   36    15    >   INIT_FCALL                                               'reset'
         16        SEND_REF                                                 !0
         17        DO_ICALL                                                 
   38    18    >   INIT_FCALL                                               'key'
         19        SEND_VAR                                                 !0
         20        DO_ICALL                                         $12     
         21        IS_NOT_IDENTICAL                                         !2, $12
         22      > JMPZ                                                     ~13, ->24
   39    23    > > RETURN                                                   <false>
   40    24    >   PRE_INC                                                  !2
   41    25        INIT_FCALL                                               'next'
         26        SEND_REF                                                 !0
         27        DO_ICALL                                                 
   42    28        IS_SMALLER                                               !2, !3
         29      > JMPNZ                                                    ~16, ->18
   43    30    > > RETURN                                                   <true>
   44    31*     > RETURN                                                   null

End of function method_5

Function method_6:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 4
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
filename:       /in/rkieX
function name:  method_6
number of ops:  15
compiled vars:  !0 = $arr, !1 = $i, !2 = $l
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
   48     1        ASSIGN                                                   !1, 0
   49     2        COUNT                                            ~4      !0
          3        ASSIGN                                                   !2, ~4
   51     4    >   BEGIN_SILENCE                                    ~6      
          5        FETCH_DIM_R                                      ~7      !0, !1
          6        END_SILENCE                                              ~6
          7        TYPE_CHECK                                    2          ~7
          8      > JMPZ                                                     ~8, ->10
   52     9    > > RETURN                                                   <false>
   53    10    >   PRE_INC                                                  !1
   54    11        IS_SMALLER                                               !1, !2
         12      > JMPNZ                                                    ~10, ->4
   55    13    > > RETURN                                                   <true>
   56    14*     > RETURN                                                   null

End of function method_6

Function method_7:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rkieX
function name:  method_7
number of ops:  13
compiled vars:  !0 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
   60     1        INIT_FCALL                                               'array_keys'
          2        INIT_FCALL                                               'array_values'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $1      
          5        SEND_VAR                                                 $1
          6        DO_ICALL                                         $2      
          7        INIT_FCALL                                               'array_keys'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $3      
         10        IS_IDENTICAL                                     ~4      $2, $3
         11      > RETURN                                                   ~4
   61    12*     > RETURN                                                   null

End of function method_7

Function method_8:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 21
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rkieX
function name:  method_8
number of ops:  27
compiled vars:  !0 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   RECV                                             !0      
   66     1        INIT_FCALL                                               'reset'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                                 
   67     4        INIT_FCALL                                               'key'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $2      
          7        IS_NOT_IDENTICAL                                         $2, 0
          8      > JMPZ                                                     ~3, ->10
   68     9    > > RETURN                                                   <false>
   70    10    >   INIT_FCALL                                               'end'
         11        SEND_REF                                                 !0
         12        DO_ICALL                                                 
   71    13        INIT_FCALL                                               'key'
         14        SEND_VAR                                                 !0
         15        DO_ICALL                                         $5      
         16        COUNT                                            ~6      !0
         17        SUB                                              ~7      ~6, 1
         18        IS_NOT_IDENTICAL                                         $5, ~7
         19      > JMPZ                                                     ~8, ->21
   72    20    > > RETURN                                                   <false>
   74    21    >   INIT_FCALL                                               'array_values'
         22        SEND_VAR                                                 !0
         23        DO_ICALL                                         $9      
         24        IS_IDENTICAL                                     ~10     !0, $9
         25      > RETURN                                                   ~10
   75    26*     > RETURN                                                   null

End of function method_8

Function benchmark:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 50
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 50
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 11
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 49
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 49
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 28
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
Branch analysis from position: 28
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 11
Branch analysis from position: 31
Branch analysis from position: 11
Branch analysis from position: 50
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 50
filename:       /in/rkieX
function name:  benchmark
number of ops:  52
compiled vars:  !0 = $methods, !1 = $target, !2 = $expected, !3 = $method, !4 = $start, !5 = $i, !6 = $end
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   77     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   78     3      > FE_RESET_R                                       $7      !0, ->50
          4    > > FE_FETCH_R                                               $7, !3, ->50
   79     5    >   INIT_FCALL                                               'microtime'
          6        SEND_VAL                                                 <true>
          7        DO_ICALL                                         $8      
          8        ASSIGN                                                   !4, $8
   80     9        ASSIGN                                                   !5, 0
         10      > JMP                                                      ->29
   82    11    >   INIT_DYNAMIC_CALL                                        !3
         12        SEND_VAR_EX                                              !1
         13        DO_FCALL                                      0  $11     
         14        IS_NOT_IDENTICAL                                         !2, $11
         15      > JMPZ                                                     ~12, ->28
   83    16    >   ROPE_INIT                                     3  ~14     'Method+'
         17        ROPE_ADD                                      1  ~14     ~14, !3
         18        ROPE_END                                      2  ~13     ~14, '+is+disqualified+for+returning+an+incorrect+result.%0A'
         19        ECHO                                                     ~13
   84    20        INIT_FCALL                                               'array_search'
         21        SEND_VAR                                                 !3
         22        SEND_VAR                                                 !0
         23        SEND_VAL                                                 <true>
         24        DO_ICALL                                         $16     
         25        UNSET_DIM                                                !0, $16
   85    26        ASSIGN                                                   !5, 0
   86    27      > JMP                                                      ->31
   80    28    >   PRE_INC                                                  !5
         29    >   IS_SMALLER                                               !5, 2000
         30      > JMPNZ                                                    ~19, ->11
   89    31    >   IS_NOT_EQUAL                                             !5, 0
         32      > JMPZ                                                     ~20, ->49
   90    33    >   INIT_FCALL                                               'microtime'
         34        SEND_VAL                                                 <true>
         35        DO_ICALL                                         $21     
         36        ASSIGN                                                   !6, $21
   91    37        ROPE_INIT                                     3  ~24     'Time+taken+with+'
         38        ROPE_ADD                                      1  ~24     ~24, !3
         39        ROPE_END                                      2  ~23     ~24, '+%3D+'
         40        INIT_FCALL                                               'round'
         41        SUB                                              ~26     !6, !4
         42        MUL                                              ~27     ~26, 1000
         43        SEND_VAL                                                 ~27
         44        SEND_VAL                                                 3
         45        DO_ICALL                                         $28     
         46        CONCAT                                           ~29     ~23, $28
         47        CONCAT                                           ~30     ~29, 'ms%0A'
         48        ECHO                                                     ~30
   78    49    > > JMP                                                      ->4
         50    >   FE_FREE                                                  $7
   94    51      > RETURN                                                   null

End of function benchmark

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.13 ms | 1489 KiB | 25 Q