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()); // Run the substr benchmark // exhaust the gc's threshhold at 10K $it = 10000; 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']); // compare the results $a = array_sum($results['substr']) / $it; $b = array_sum($results['strpos']) / $it; printf("substr: %.6fs/iteration @ %d iterations\nstrpos: %.6fs/iteration @ %d iterations\n\n", $a, $it, $b, $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"); }
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
2 jumps found. (Code = 43) Position 1 = 81, Position 2 = 88
Branch analysis from position: 81
1 jumps found. (Code = 42) Position 1 = 100
Branch analysis from position: 100
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 88
2 jumps found. (Code = 43) Position 1 = 90, Position 2 = 97
Branch analysis from position: 90
1 jumps found. (Code = 42) Position 1 = 100
Branch analysis from position: 100
Branch analysis from position: 97
1 jumps found. (Code = 62) Position 1 = -2
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/0Wupn
function name:  (null)
number of ops:  101
compiled vars:  !0 = $haystack, !1 = $needle, !2 = $results, !3 = $it, !4 = $i, !5 = $t, !6 = $a, !7 = $b
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>
   20     3        ASSIGN                                                   !3, 10000
   22     4        ASSIGN                                                   !4, 0
          5      > JMP                                                      ->22
   23     6    >   INIT_FCALL                                               'microtime'
          7        SEND_VAL                                                 <true>
          8        DO_ICALL                                         $13     
          9        ASSIGN                                                   !5, $13
   24    10        INIT_FCALL                                               'starts_with_substr'
         11        SEND_VAR                                                 !0
         12        SEND_VAR                                                 !1
         13        DO_FCALL                                      0          
   25    14        INIT_FCALL                                               'microtime'
         15        SEND_VAL                                                 <true>
         16        DO_ICALL                                         $18     
         17        SUB                                              ~19     $18, !5
         18        FETCH_DIM_W                                      $16     !2, 'substr'
         19        ASSIGN_DIM                                               $16
         20        OP_DATA                                                  ~19
   22    21        PRE_INC                                                  !4
         22    >   IS_SMALLER                                               !4, !3
         23      > JMPNZ                                                    ~21, ->6
   29    24    >   INIT_FCALL                                               'array_pop'
         25        FETCH_DIM_W                                      $22     !2, 'substr'
         26        SEND_REF                                                 $22
         27        DO_ICALL                                                 
   30    28        INIT_FCALL                                               'array_shift'
         29        FETCH_DIM_W                                      $24     !2, 'substr'
         30        SEND_REF                                                 $24
         31        DO_ICALL                                                 
   35    32        ASSIGN                                                   !4, 0
         33      > JMP                                                      ->50
   36    34    >   INIT_FCALL                                               'microtime'
         35        SEND_VAL                                                 <true>
         36        DO_ICALL                                         $27     
         37        ASSIGN                                                   !5, $27
   37    38        INIT_FCALL                                               'starts_with_strpos'
         39        SEND_VAR                                                 !0
         40        SEND_VAR                                                 !1
         41        DO_FCALL                                      0          
   38    42        INIT_FCALL                                               'microtime'
         43        SEND_VAL                                                 <true>
         44        DO_ICALL                                         $32     
         45        SUB                                              ~33     $32, !5
         46        FETCH_DIM_W                                      $30     !2, 'strpos'
         47        ASSIGN_DIM                                               $30
         48        OP_DATA                                                  ~33
   35    49        PRE_INC                                                  !4
         50    >   IS_SMALLER                                               !4, !3
         51      > JMPNZ                                                    ~35, ->34
   42    52    >   INIT_FCALL                                               'array_pop'
         53        FETCH_DIM_W                                      $36     !2, 'strpos'
         54        SEND_REF                                                 $36
         55        DO_ICALL                                                 
   43    56        INIT_FCALL                                               'array_shift'
         57        FETCH_DIM_W                                      $38     !2, 'strpos'
         58        SEND_REF                                                 $38
         59        DO_ICALL                                                 
   47    60        INIT_FCALL                                               'array_sum'
         61        FETCH_DIM_R                                      ~40     !2, 'substr'
         62        SEND_VAL                                                 ~40
         63        DO_ICALL                                         $41     
         64        DIV                                              ~42     $41, !3
         65        ASSIGN                                                   !6, ~42
   48    66        INIT_FCALL                                               'array_sum'
         67        FETCH_DIM_R                                      ~44     !2, 'strpos'
         68        SEND_VAL                                                 ~44
         69        DO_ICALL                                         $45     
         70        DIV                                              ~46     $45, !3
         71        ASSIGN                                                   !7, ~46
   50    72        INIT_FCALL                                               'printf'
         73        SEND_VAL                                                 'substr%3A+%25.6fs%2Fiteration+%40+%25d+iterations%0Astrpos%3A+%25.6fs%2Fiteration+%40+%25d+iterations%0A%0A'
         74        SEND_VAR                                                 !6
         75        SEND_VAR                                                 !3
         76        SEND_VAR                                                 !7
         77        SEND_VAR                                                 !3
         78        DO_ICALL                                                 
   53    79        IS_SMALLER                                               !7, !6
         80      > JMPZ                                                     ~49, ->88
   54    81    >   INIT_FCALL                                               'printf'
         82        SEND_VAL                                                 'strpos+is+%25d%25%25+faster+than+substr%0A'
         83        DIV                                              ~50     !7, !6
         84        MUL                                              ~51     ~50, 100
         85        SEND_VAL                                                 ~51
         86        DO_ICALL                                                 
         87      > JMP                                                      ->100
   55    88    >   IS_SMALLER                                               !6, !7
         89      > JMPZ                                                     ~53, ->97
   56    90    >   INIT_FCALL                                               'printf'
         91        SEND_VAL                                                 'substr+is+%25d%25%25+faster+than+strpos%0A'
         92        DIV                                              ~54     !6, !7
         93        MUL                                              ~55     ~54, 100
         94        SEND_VAL                                                 ~55
         95        DO_ICALL                                                 
         96      > JMP                                                      ->100
   58    97    >   INIT_FCALL                                               'printf'
         98        SEND_VAL                                                 'strpos+and+substr+are+the+same%0A'
         99        DO_ICALL                                                 
   59   100    > > RETURN                                                   1

Function starts_with_strpos:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0Wupn
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/0Wupn
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:
186.24 ms | 1411 KiB | 29 Q