3v4l.org

run code in 300+ PHP versions simultaneously
<?php function starts_with_strpos($haystack, $needle) { return strpos($haystack, $needle) === 0; } function starts_with_substr($haystack, $needle) { return substr($haystack, 0, strlen($needle)) === $needle; } // setup the haystack and needle $haystack = "Hello PHP"; $needle = "Hello"; // initialize the results array $results = array('substr' => array(), 'strpos' => array(), 'strpos_without_function' => array()); // exhaust the gc's threshhold at 10K $it = 10000; // Run the substr benchmark for($i = 0; $i < $it; $i++) { $t = microtime(true); starts_with_substr($haystack, $needle); $results['substr'][] = microtime(true) - $t; } // discard the first and last results as opline biased array_pop($results['substr']); array_shift($results['substr']); // Run the strpos benchmark for($i = 0; $i < $it; $i++) { $t = microtime(true); starts_with_strpos($haystack, $needle); $results['strpos'][] = microtime(true) - $t; } // discard the first and last results as opline biased array_pop($results['strpos']); array_shift($results['strpos']); // Run the strpos without a wrapper function benchmark for($i = 0; $i < $it; $i++) { $t = microtime(true); $x = strpos($haystack, $needle) === 0; $results['strpos_without_function'][] = microtime(true) - $t; } // discard the first and last results as opline biased array_pop($results['strpos_without_function']); array_shift($results['strpos_without_function']); // compare the results $a = array_sum($results['substr']) / $it; $b = array_sum($results['strpos']) / $it; $c = array_sum($results['strpos_without_function']) / $it; printf("substr: %.9fs/iteration @ %d iterations\nstrpos: %.9fs/iteration @ %d iterations\nstrpos (without wrapper): %.9fs/iteration @ %d iterations\n\n", $a, $it, $b, $it, $c, $it); // determine which is faster (substr or strpos) if ($a > $b) { // substr is slower printf("strpos is %d%% faster than substr\n", $b / $a * 100); } elseif ($b > $a) { // strpos is slower printf("substr is %d%% faster than strpos\n", $a / $b * 100); } else { // they're the same printf("strpos and substr are the same\n"); } // determine which is faster (strpos with a wrapper or without) if ($b > $c) { // strpos wrapper is slower printf("strpos without a wrapper function is %d%% faster than strpos with a wrapper function\n", $c / $b * 100); } elseif ($c > $b) { // strpos wrapper is faster printf("strpos with a wrapper function is %d%% faster than strpos without a wrapper fucntion\n", $b / $c * 100); } else { // they're the same printf("strpos and substr are the same\n"); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
2 jumps found. (Code = 44) Position 1 = 24, Position 2 = 6
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
2 jumps found. (Code = 44) Position 1 = 52, Position 2 = 34
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 80
Branch analysis from position: 80
2 jumps found. (Code = 44) Position 1 = 82, Position 2 = 62
Branch analysis from position: 82
2 jumps found. (Code = 43) Position 1 = 119, Position 2 = 126
Branch analysis from position: 119
1 jumps found. (Code = 42) Position 1 = 138
Branch analysis from position: 138
2 jumps found. (Code = 43) Position 1 = 140, Position 2 = 147
Branch analysis from position: 140
1 jumps found. (Code = 42) Position 1 = 159
Branch analysis from position: 159
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 147
2 jumps found. (Code = 43) Position 1 = 149, Position 2 = 156
Branch analysis from position: 149
1 jumps found. (Code = 42) Position 1 = 159
Branch analysis from position: 159
Branch analysis from position: 156
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 126
2 jumps found. (Code = 43) Position 1 = 128, Position 2 = 135
Branch analysis from position: 128
1 jumps found. (Code = 42) Position 1 = 138
Branch analysis from position: 138
Branch analysis from position: 135
2 jumps found. (Code = 43) Position 1 = 140, Position 2 = 147
Branch analysis from position: 140
Branch analysis from position: 147
Branch analysis from position: 62
2 jumps found. (Code = 44) Position 1 = 82, Position 2 = 62
Branch analysis from position: 82
Branch analysis from position: 62
Branch analysis from position: 34
2 jumps found. (Code = 44) Position 1 = 52, Position 2 = 34
Branch analysis from position: 52
Branch analysis from position: 34
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 24, Position 2 = 6
Branch analysis from position: 24
Branch analysis from position: 6
filename:       /in/K7ERC
function name:  (null)
number of ops:  160
compiled vars:  !0 = $haystack, !1 = $needle, !2 = $results, !3 = $it, !4 = $i, !5 = $t, !6 = $x, !7 = $a, !8 = $b, !9 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   ASSIGN                                                   !0, 'Hello+PHP'
   12     1        ASSIGN                                                   !1, 'Hello'
   15     2        ASSIGN                                                   !2, <array>
   18     3        ASSIGN                                                   !3, 10000
   23     4        ASSIGN                                                   !4, 0
          5      > JMP                                                      ->22
   24     6    >   INIT_FCALL                                               'microtime'
          7        SEND_VAL                                                 <true>
          8        DO_ICALL                                         $15     
          9        ASSIGN                                                   !5, $15
   25    10        INIT_FCALL                                               'starts_with_substr'
         11        SEND_VAR                                                 !0
         12        SEND_VAR                                                 !1
         13        DO_FCALL                                      0          
   26    14        INIT_FCALL                                               'microtime'
         15        SEND_VAL                                                 <true>
         16        DO_ICALL                                         $20     
         17        SUB                                              ~21     $20, !5
         18        FETCH_DIM_W                                      $18     !2, 'substr'
         19        ASSIGN_DIM                                               $18
         20        OP_DATA                                                  ~21
   23    21        PRE_INC                                                  !4
         22    >   IS_SMALLER                                               !4, !3
         23      > JMPNZ                                                    ~23, ->6
   30    24    >   INIT_FCALL                                               'array_pop'
         25        FETCH_DIM_W                                      $24     !2, 'substr'
         26        SEND_REF                                                 $24
         27        DO_ICALL                                                 
   31    28        INIT_FCALL                                               'array_shift'
         29        FETCH_DIM_W                                      $26     !2, 'substr'
         30        SEND_REF                                                 $26
         31        DO_ICALL                                                 
   36    32        ASSIGN                                                   !4, 0
         33      > JMP                                                      ->50
   37    34    >   INIT_FCALL                                               'microtime'
         35        SEND_VAL                                                 <true>
         36        DO_ICALL                                         $29     
         37        ASSIGN                                                   !5, $29
   38    38        INIT_FCALL                                               'starts_with_strpos'
         39        SEND_VAR                                                 !0
         40        SEND_VAR                                                 !1
         41        DO_FCALL                                      0          
   39    42        INIT_FCALL                                               'microtime'
         43        SEND_VAL                                                 <true>
         44        DO_ICALL                                         $34     
         45        SUB                                              ~35     $34, !5
         46        FETCH_DIM_W                                      $32     !2, 'strpos'
         47        ASSIGN_DIM                                               $32
         48        OP_DATA                                                  ~35
   36    49        PRE_INC                                                  !4
         50    >   IS_SMALLER                                               !4, !3
         51      > JMPNZ                                                    ~37, ->34
   43    52    >   INIT_FCALL                                               'array_pop'
         53        FETCH_DIM_W                                      $38     !2, 'strpos'
         54        SEND_REF                                                 $38
         55        DO_ICALL                                                 
   44    56        INIT_FCALL                                               'array_shift'
         57        FETCH_DIM_W                                      $40     !2, 'strpos'
         58        SEND_REF                                                 $40
         59        DO_ICALL                                                 
   49    60        ASSIGN                                                   !4, 0
         61      > JMP                                                      ->80
   50    62    >   INIT_FCALL                                               'microtime'
         63        SEND_VAL                                                 <true>
         64        DO_ICALL                                         $43     
         65        ASSIGN                                                   !5, $43
   51    66        INIT_FCALL                                               'strpos'
         67        SEND_VAR                                                 !0
         68        SEND_VAR                                                 !1
         69        DO_ICALL                                         $45     
         70        IS_IDENTICAL                                     ~46     $45, 0
         71        ASSIGN                                                   !6, ~46
   52    72        INIT_FCALL                                               'microtime'
         73        SEND_VAL                                                 <true>
         74        DO_ICALL                                         $50     
         75        SUB                                              ~51     $50, !5
         76        FETCH_DIM_W                                      $48     !2, 'strpos_without_function'
         77        ASSIGN_DIM                                               $48
         78        OP_DATA                                                  ~51
   49    79        PRE_INC                                                  !4
         80    >   IS_SMALLER                                               !4, !3
         81      > JMPNZ                                                    ~53, ->62
   56    82    >   INIT_FCALL                                               'array_pop'
         83        FETCH_DIM_W                                      $54     !2, 'strpos_without_function'
         84        SEND_REF                                                 $54
         85        DO_ICALL                                                 
   57    86        INIT_FCALL                                               'array_shift'
         87        FETCH_DIM_W                                      $56     !2, 'strpos_without_function'
         88        SEND_REF                                                 $56
         89        DO_ICALL                                                 
   61    90        INIT_FCALL                                               'array_sum'
         91        FETCH_DIM_R                                      ~58     !2, 'substr'
         92        SEND_VAL                                                 ~58
         93        DO_ICALL                                         $59     
         94        DIV                                              ~60     $59, !3
         95        ASSIGN                                                   !7, ~60
   62    96        INIT_FCALL                                               'array_sum'
         97        FETCH_DIM_R                                      ~62     !2, 'strpos'
         98        SEND_VAL                                                 ~62
         99        DO_ICALL                                         $63     
        100        DIV                                              ~64     $63, !3
        101        ASSIGN                                                   !8, ~64
   63   102        INIT_FCALL                                               'array_sum'
        103        FETCH_DIM_R                                      ~66     !2, 'strpos_without_function'
        104        SEND_VAL                                                 ~66
        105        DO_ICALL                                         $67     
        106        DIV                                              ~68     $67, !3
        107        ASSIGN                                                   !9, ~68
   65   108        INIT_FCALL                                               'printf'
        109        SEND_VAL                                                 'substr%3A+%25.9fs%2Fiteration+%40+%25d+iterations%0Astrpos%3A+%25.9fs%2Fiteration+%40+%25d+iterations%0Astrpos+%28without+wrapper%29%3A+%25.9fs%2Fiteration+%40+%25d+iterations%0A%0A'
        110        SEND_VAR                                                 !7
        111        SEND_VAR                                                 !3
        112        SEND_VAR                                                 !8
        113        SEND_VAR                                                 !3
        114        SEND_VAR                                                 !9
        115        SEND_VAR                                                 !3
        116        DO_ICALL                                                 
   68   117        IS_SMALLER                                               !8, !7
        118      > JMPZ                                                     ~71, ->126
   69   119    >   INIT_FCALL                                               'printf'
        120        SEND_VAL                                                 'strpos+is+%25d%25%25+faster+than+substr%0A'
        121        DIV                                              ~72     !8, !7
        122        MUL                                              ~73     ~72, 100
        123        SEND_VAL                                                 ~73
        124        DO_ICALL                                                 
        125      > JMP                                                      ->138
   70   126    >   IS_SMALLER                                               !7, !8
        127      > JMPZ                                                     ~75, ->135
   71   128    >   INIT_FCALL                                               'printf'
        129        SEND_VAL                                                 'substr+is+%25d%25%25+faster+than+strpos%0A'
        130        DIV                                              ~76     !7, !8
        131        MUL                                              ~77     ~76, 100
        132        SEND_VAL                                                 ~77
        133        DO_ICALL                                                 
        134      > JMP                                                      ->138
   73   135    >   INIT_FCALL                                               'printf'
        136        SEND_VAL                                                 'strpos+and+substr+are+the+same%0A'
        137        DO_ICALL                                                 
   77   138    >   IS_SMALLER                                               !9, !8
        139      > JMPZ                                                     ~80, ->147
   78   140    >   INIT_FCALL                                               'printf'
        141        SEND_VAL                                                 'strpos+without+a+wrapper+function+is+%25d%25%25+faster+than+strpos+with+a+wrapper+function%0A'
        142        DIV                                              ~81     !9, !8
        143        MUL                                              ~82     ~81, 100
        144        SEND_VAL                                                 ~82
        145        DO_ICALL                                                 
        146      > JMP                                                      ->159
   79   147    >   IS_SMALLER                                               !8, !9
        148      > JMPZ                                                     ~84, ->156
   80   149    >   INIT_FCALL                                               'printf'
        150        SEND_VAL                                                 'strpos+with+a+wrapper+function+is+%25d%25%25+faster+than+strpos+without+a+wrapper+fucntion%0A'
        151        DIV                                              ~85     !8, !9
        152        MUL                                              ~86     ~85, 100
        153        SEND_VAL                                                 ~86
        154        DO_ICALL                                                 
        155      > JMP                                                      ->159
   82   156    >   INIT_FCALL                                               'printf'
        157        SEND_VAL                                                 'strpos+and+substr+are+the+same%0A'
        158        DO_ICALL                                                 
   83   159    > > RETURN                                                   1

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

End of function starts_with_strpos

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

End of function starts_with_substr

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152 ms | 1414 KiB | 29 Q