3v4l.org

run code in 300+ PHP versions simultaneously
#!/usr/bin/php <?php declare(strict_types=1); const ITERATIONS = 100000; $results = array(); for ($i = 0; $i < ITERATIONS; ++$i) { // heat up cpu (if it was powersaving-running-low-cus-nothing-was-happening, most modern cpus try to do it by default) } function array_is_list_post(array $array): bool { $i = 0; foreach ($array as $k => $v) { if ($k !== $i++) { return false; } } return true; } function array_is_list_pre(array $array): bool { $i = -1; foreach ($array as $k => $v) { ++$i; if ($k !== $i) { return false; } } return true; } $l1 = str_split(str_repeat("A", 300), 1); $t = microtime(true); for ($i = 0; $i < ITERATIONS; ++$i) { array_is_list_pre($l1); } $t = microtime(true) - $t; $results["pre"] = $t; $t = microtime(true); for ($i = 0; $i < ITERATIONS; ++$i) { array_is_list_post($l1); } $t = microtime(true) - $t; $results["post"] = $t; if ($results["pre"] === $results["post"]) { echo "it's a tie!"; } elseif ($results["pre"] < $results["post"]) { echo "pre is faster!"; } else { echo "post is faster!"; } echo "diff: " . abs(($results["pre"] - $results["post"])) . "\n"; var_dump($results); exit();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 8, Position 2 = 4
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 23
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
2 jumps found. (Code = 44) Position 1 = 50, Position 2 = 43
Branch analysis from position: 50
2 jumps found. (Code = 43) Position 1 = 61, Position 2 = 63
Branch analysis from position: 61
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 63
2 jumps found. (Code = 43) Position 1 = 67, Position 2 = 69
Branch analysis from position: 67
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
Branch analysis from position: 69
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 43
2 jumps found. (Code = 44) Position 1 = 50, Position 2 = 43
Branch analysis from position: 50
Branch analysis from position: 43
Branch analysis from position: 23
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 23
Branch analysis from position: 30
Branch analysis from position: 23
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 8, Position 2 = 4
Branch analysis from position: 8
Branch analysis from position: 4
filename:       /in/9BPqL
function name:  (null)
number of ops:  84
compiled vars:  !0 = $results, !1 = $i, !2 = $l1, !3 = $t
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   DECLARE_CONST                                            'ITERATIONS', 100000
    7     1        ASSIGN                                                   !0, <array>
    8     2        ASSIGN                                                   !1, 0
          3      > JMP                                                      ->5
          4    >   PRE_INC                                                  !1
          5    >   FETCH_CONSTANT                                   ~7      'ITERATIONS'
          6        IS_SMALLER                                               !1, ~7
          7      > JMPNZ                                                    ~8, ->4
   33     8    >   INIT_FCALL                                               'str_split'
          9        INIT_FCALL                                               'str_repeat'
         10        SEND_VAL                                                 'A'
         11        SEND_VAL                                                 300
         12        DO_ICALL                                         $9      
         13        SEND_VAR                                                 $9
         14        SEND_VAL                                                 1
         15        DO_ICALL                                         $10     
         16        ASSIGN                                                   !2, $10
   34    17        INIT_FCALL                                               'microtime'
         18        SEND_VAL                                                 <true>
         19        DO_ICALL                                         $12     
         20        ASSIGN                                                   !3, $12
   35    21        ASSIGN                                                   !1, 0
         22      > JMP                                                      ->27
   36    23    >   INIT_FCALL                                               'array_is_list_pre'
         24        SEND_VAR                                                 !2
         25        DO_FCALL                                      0          
   35    26        PRE_INC                                                  !1
         27    >   FETCH_CONSTANT                                   ~17     'ITERATIONS'
         28        IS_SMALLER                                               !1, ~17
         29      > JMPNZ                                                    ~18, ->23
   38    30    >   INIT_FCALL                                               'microtime'
         31        SEND_VAL                                                 <true>
         32        DO_ICALL                                         $19     
         33        SUB                                              ~20     $19, !3
         34        ASSIGN                                                   !3, ~20
   39    35        ASSIGN_DIM                                               !0, 'pre'
         36        OP_DATA                                                  !3
   40    37        INIT_FCALL                                               'microtime'
         38        SEND_VAL                                                 <true>
         39        DO_ICALL                                         $23     
         40        ASSIGN                                                   !3, $23
   41    41        ASSIGN                                                   !1, 0
         42      > JMP                                                      ->47
   42    43    >   INIT_FCALL                                               'array_is_list_post'
         44        SEND_VAR                                                 !2
         45        DO_FCALL                                      0          
   41    46        PRE_INC                                                  !1
         47    >   FETCH_CONSTANT                                   ~28     'ITERATIONS'
         48        IS_SMALLER                                               !1, ~28
         49      > JMPNZ                                                    ~29, ->43
   44    50    >   INIT_FCALL                                               'microtime'
         51        SEND_VAL                                                 <true>
         52        DO_ICALL                                         $30     
         53        SUB                                              ~31     $30, !3
         54        ASSIGN                                                   !3, ~31
   45    55        ASSIGN_DIM                                               !0, 'post'
         56        OP_DATA                                                  !3
   46    57        FETCH_DIM_R                                      ~34     !0, 'pre'
         58        FETCH_DIM_R                                      ~35     !0, 'post'
         59        IS_IDENTICAL                                             ~34, ~35
         60      > JMPZ                                                     ~36, ->63
   47    61    >   ECHO                                                     'it%27s+a+tie%21'
         62      > JMP                                                      ->70
   48    63    >   FETCH_DIM_R                                      ~37     !0, 'pre'
         64        FETCH_DIM_R                                      ~38     !0, 'post'
         65        IS_SMALLER                                               ~37, ~38
         66      > JMPZ                                                     ~39, ->69
   49    67    >   ECHO                                                     'pre+is+faster%21'
         68      > JMP                                                      ->70
   51    69    >   ECHO                                                     'post+is+faster%21'
   53    70    >   INIT_FCALL                                               'abs'
         71        FETCH_DIM_R                                      ~40     !0, 'pre'
         72        FETCH_DIM_R                                      ~41     !0, 'post'
         73        SUB                                              ~42     ~40, ~41
         74        SEND_VAL                                                 ~42
         75        DO_ICALL                                         $43     
         76        CONCAT                                           ~44     'diff%3A+', $43
         77        CONCAT                                           ~45     ~44, '%0A'
         78        ECHO                                                     ~45
   54    79        INIT_FCALL                                               'var_dump'
         80        SEND_VAR                                                 !0
         81        DO_ICALL                                                 
   55    82      > EXIT                                                     
         83*     > RETURN                                                   1

Function array_is_list_post:
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/9BPqL
function name:  array_is_list_post
number of ops:  15
compiled vars:  !0 = $array, !1 = $i, !2 = $v, !3 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
   13     1        ASSIGN                                                   !1, 0
   14     2      > FE_RESET_R                                       $5      !0, ->11
          3    > > FE_FETCH_R                                       ~6      $5, !2, ->11
          4    >   ASSIGN                                                   !3, ~6
   15     5        POST_INC                                         ~8      !1
          6        IS_NOT_IDENTICAL                                         !3, ~8
          7      > JMPZ                                                     ~9, ->10
   16     8    >   FE_FREE                                                  $5
          9      > RETURN                                                   <false>
   14    10    > > JMP                                                      ->3
         11    >   FE_FREE                                                  $5
   19    12      > RETURN                                                   <true>
   20    13*       VERIFY_RETURN_TYPE                                       
         14*     > RETURN                                                   null

End of function array_is_list_post

Function array_is_list_pre:
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/9BPqL
function name:  array_is_list_pre
number of ops:  15
compiled vars:  !0 = $array, !1 = $i, !2 = $v, !3 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   23     1        ASSIGN                                                   !1, -1
   24     2      > FE_RESET_R                                       $5      !0, ->11
          3    > > FE_FETCH_R                                       ~6      $5, !2, ->11
          4    >   ASSIGN                                                   !3, ~6
   25     5        PRE_INC                                                  !1
   26     6        IS_NOT_IDENTICAL                                         !3, !1
          7      > JMPZ                                                     ~9, ->10
   27     8    >   FE_FREE                                                  $5
          9      > RETURN                                                   <false>
   24    10    > > JMP                                                      ->3
         11    >   FE_FREE                                                  $5
   30    12      > RETURN                                                   <true>
   31    13*       VERIFY_RETURN_TYPE                                       
         14*     > RETURN                                                   null

End of function array_is_list_pre

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.44 ms | 1415 KiB | 25 Q