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); 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/3NC2m
function name:  (null)
number of ops:  160
compiled vars:  !0 = $haystack, !1 = $needle, !2 = $results, !3 = $it, !4 = $i, !5 = $t, !6 = $a, !7 = $b, !8 = $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                                         $14     
          9        ASSIGN                                                   !5, $14
   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                                         $19     
         17        SUB                                              ~20     $19, !5
         18        FETCH_DIM_W                                      $17     !2, 'substr'
         19        ASSIGN_DIM                                               $17
         20        OP_DATA                                                  ~20
   23    21        PRE_INC                                                  !4
         22    >   IS_SMALLER                                               !4, !3
         23      > JMPNZ                                                    ~22, ->6
   30    24    >   INIT_FCALL                                               'array_pop'
         25        FETCH_DIM_W                                      $23     !2, 'substr'
         26        SEND_REF                                                 $23
         27        DO_ICALL                                                 
   31    28        INIT_FCALL                                               'array_shift'
         29        FETCH_DIM_W                                      $25     !2, 'substr'
         30        SEND_REF                                                 $25
         31        DO_ICALL                                                 
   36    32        ASSIGN                                                   !4, 0
         33      > JMP                                                      ->50
   37    34    >   INIT_FCALL                                               'microtime'
         35        SEND_VAL                                                 <true>
         36        DO_ICALL                                         $28     
         37        ASSIGN                                                   !5, $28
   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                                         $33     
         45        SUB                                              ~34     $33, !5
         46        FETCH_DIM_W                                      $31     !2, 'strpos'
         47        ASSIGN_DIM                                               $31
         48        OP_DATA                                                  ~34
   36    49        PRE_INC                                                  !4
         50    >   IS_SMALLER                                               !4, !3
         51      > JMPNZ                                                    ~36, ->34
   43    52    >   INIT_FCALL                                               'array_pop'
         53        FETCH_DIM_W                                      $37     !2, 'strpos'
         54        SEND_REF                                                 $37
         55        DO_ICALL                                                 
   44    56        INIT_FCALL                                               'array_shift'
         57        FETCH_DIM_W                                      $39     !2, 'strpos'
         58        SEND_REF                                                 $39
         59        DO_ICALL                                                 
   49    60        ASSIGN                                                   !4, 0
         61      > JMP                                                      ->80
   50    62    >   INIT_FCALL                                               'microtime'
         63        SEND_VAL                                                 <true>
         64        DO_ICALL                                         $42     
         65        ASSIGN                                                   !5, $42
   51    66        INIT_FCALL                                               'strpos'
         67        SEND_VAR                                                 !0
         68        SEND_VAR                                                 !1
         69        DO_ICALL                                         $44     
         70        IS_IDENTICAL                                     ~45     $44, 0
         71        FREE                                                     ~45
   52    72        INIT_FCALL                                               'microtime'
         73        SEND_VAL                                                 <true>
         74        DO_ICALL                                         $48     
         75        SUB                                              ~49     $48, !5
         76        FETCH_DIM_W                                      $46     !2, 'strpos_without_function'
         77        ASSIGN_DIM                                               $46
         78        OP_DATA                                                  ~49
   49    79        PRE_INC                                                  !4
         80    >   IS_SMALLER                                               !4, !3
         81      > JMPNZ                                                    ~51, ->62
   56    82    >   INIT_FCALL                                               'array_pop'
         83        FETCH_DIM_W                                      $52     !2, 'strpos_without_function'
         84        SEND_REF                                                 $52
         85        DO_ICALL                                                 
   57    86        INIT_FCALL                                               'array_shift'
         87        FETCH_DIM_W                                      $54     !2, 'strpos_without_function'
         88        SEND_REF                                                 $54
         89        DO_ICALL                                                 
   61    90        INIT_FCALL                                               'array_sum'
         91        FETCH_DIM_R                                      ~56     !2, 'substr'
         92        SEND_VAL                                                 ~56
         93        DO_ICALL                                         $57     
         94        DIV                                              ~58     $57, !3
         95        ASSIGN                                                   !6, ~58
   62    96        INIT_FCALL                                               'array_sum'
         97        FETCH_DIM_R                                      ~60     !2, 'strpos'
         98        SEND_VAL                                                 ~60
         99        DO_ICALL                                         $61     
        100        DIV                                              ~62     $61, !3
        101        ASSIGN                                                   !7, ~62
   63   102        INIT_FCALL                                               'array_sum'
        103        FETCH_DIM_R                                      ~64     !2, 'strpos_without_function'
        104        SEND_VAL                                                 ~64
        105        DO_ICALL                                         $65     
        106        DIV                                              ~66     $65, !3
        107        ASSIGN                                                   !8, ~66
   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                                                 !6
        111        SEND_VAR                                                 !3
        112        SEND_VAR                                                 !7
        113        SEND_VAR                                                 !3
        114        SEND_VAR                                                 !8
        115        SEND_VAR                                                 !3
        116        DO_ICALL                                                 
   68   117        IS_SMALLER                                               !7, !6
        118      > JMPZ                                                     ~69, ->126
   69   119    >   INIT_FCALL                                               'printf'
        120        SEND_VAL                                                 'strpos+is+%25d%25%25+faster+than+substr%0A'
        121        DIV                                              ~70     !7, !6
        122        MUL                                              ~71     ~70, 100
        123        SEND_VAL                                                 ~71
        124        DO_ICALL                                                 
        125      > JMP                                                      ->138
   70   126    >   IS_SMALLER                                               !6, !7
        127      > JMPZ                                                     ~73, ->135
   71   128    >   INIT_FCALL                                               'printf'
        129        SEND_VAL                                                 'substr+is+%25d%25%25+faster+than+strpos%0A'
        130        DIV                                              ~74     !6, !7
        131        MUL                                              ~75     ~74, 100
        132        SEND_VAL                                                 ~75
        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                                               !8, !7
        139      > JMPZ                                                     ~78, ->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                                              ~79     !8, !7
        143        MUL                                              ~80     ~79, 100
        144        SEND_VAL                                                 ~80
        145        DO_ICALL                                                 
        146      > JMP                                                      ->159
   79   147    >   IS_SMALLER                                               !7, !8
        148      > JMPZ                                                     ~82, ->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                                              ~83     !7, !8
        152        MUL                                              ~84     ~83, 100
        153        SEND_VAL                                                 ~84
        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/3NC2m
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/3NC2m
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:
147.16 ms | 1415 KiB | 29 Q