3v4l.org

run code in 300+ PHP versions simultaneously
<?php if ( ! function_exists( 'str_starts_with' ) ) { function str_starts_with( $haystack, $needle ) { if ( '' === $needle ) { return true; } return 0 === strpos( $haystack, $needle ); } } $string = 'abcdefghijklmnopqrstuvwxyz'; $start = hrtime(true); for ( $i = 0; $i < 1000000; $i++ ) { $result1 = str_starts_with( $string, 'abc' ); $result2 = str_starts_with( $string, 'abd' ); } $end = hrtime(true); $str_starts_with_time = $end - $start; $start = hrtime(true); for ( $i = 0; $i < 1000000; $i++ ) { $result1 = 0 === strpos( $string, 'abc' ); $result2 = 0 === strpos( $string, 'abd' ); } $end = hrtime(true); $strpos_time = $end - $start; echo 'str_starts_with: ' . $str_starts_with_time . PHP_EOL; echo 'strpos: ' . $strpos_time . PHP_EOL; // echo which function is faster if ( $str_starts_with_time < $strpos_time ) { echo 'str_starts_with is faster' . PHP_EOL; } else { echo 'strpos is faster' . PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
2 jumps found. (Code = 44) Position 1 = 26, Position 2 = 13
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 51
Branch analysis from position: 51
2 jumps found. (Code = 44) Position 1 = 53, Position 2 = 38
Branch analysis from position: 53
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
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 69
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 53, Position 2 = 38
Branch analysis from position: 53
Branch analysis from position: 38
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 26, Position 2 = 13
Branch analysis from position: 26
Branch analysis from position: 13
Branch analysis from position: 6
filename:       /in/dUbZ9
function name:  (null)
number of ops:  71
compiled vars:  !0 = $string, !1 = $start, !2 = $i, !3 = $result1, !4 = $result2, !5 = $end, !6 = $str_starts_with_time, !7 = $strpos_time
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'function_exists'
          1        SEND_VAL                                                 'str_starts_with'
          2        DO_ICALL                                         $8      
          3        BOOL_NOT                                         ~9      $8
          4      > JMPZ                                                     ~9, ->6
    4     5    >   DECLARE_FUNCTION                                         'str_starts_with'
   12     6    >   ASSIGN                                                   !0, 'abcdefghijklmnopqrstuvwxyz'
   14     7        INIT_FCALL                                               'hrtime'
          8        SEND_VAL                                                 <true>
          9        DO_ICALL                                         $11     
         10        ASSIGN                                                   !1, $11
   16    11        ASSIGN                                                   !2, 0
         12      > JMP                                                      ->24
   17    13    >   INIT_FCALL                                               'str_starts_with'
         14        SEND_VAR                                                 !0
         15        SEND_VAL                                                 'abc'
         16        DO_ICALL                                         $14     
         17        ASSIGN                                                   !3, $14
   18    18        INIT_FCALL                                               'str_starts_with'
         19        SEND_VAR                                                 !0
         20        SEND_VAL                                                 'abd'
         21        DO_ICALL                                         $16     
         22        ASSIGN                                                   !4, $16
   16    23        PRE_INC                                                  !2
         24    >   IS_SMALLER                                               !2, 1000000
         25      > JMPNZ                                                    ~19, ->13
   21    26    >   INIT_FCALL                                               'hrtime'
         27        SEND_VAL                                                 <true>
         28        DO_ICALL                                         $20     
         29        ASSIGN                                                   !5, $20
   23    30        SUB                                              ~22     !5, !1
         31        ASSIGN                                                   !6, ~22
   25    32        INIT_FCALL                                               'hrtime'
         33        SEND_VAL                                                 <true>
         34        DO_ICALL                                         $24     
         35        ASSIGN                                                   !1, $24
   27    36        ASSIGN                                                   !2, 0
         37      > JMP                                                      ->51
   28    38    >   INIT_FCALL                                               'strpos'
         39        SEND_VAR                                                 !0
         40        SEND_VAL                                                 'abc'
         41        DO_ICALL                                         $27     
         42        IS_IDENTICAL                                     ~28     $27, 0
         43        ASSIGN                                                   !3, ~28
   29    44        INIT_FCALL                                               'strpos'
         45        SEND_VAR                                                 !0
         46        SEND_VAL                                                 'abd'
         47        DO_ICALL                                         $30     
         48        IS_IDENTICAL                                     ~31     $30, 0
         49        ASSIGN                                                   !4, ~31
   27    50        PRE_INC                                                  !2
         51    >   IS_SMALLER                                               !2, 1000000
         52      > JMPNZ                                                    ~34, ->38
   32    53    >   INIT_FCALL                                               'hrtime'
         54        SEND_VAL                                                 <true>
         55        DO_ICALL                                         $35     
         56        ASSIGN                                                   !5, $35
   34    57        SUB                                              ~37     !5, !1
         58        ASSIGN                                                   !7, ~37
   36    59        CONCAT                                           ~39     'str_starts_with%3A+', !6
         60        CONCAT                                           ~40     ~39, '%0A'
         61        ECHO                                                     ~40
   37    62        CONCAT                                           ~41     'strpos%3A+', !7
         63        CONCAT                                           ~42     ~41, '%0A'
         64        ECHO                                                     ~42
   40    65        IS_SMALLER                                               !6, !7
         66      > JMPZ                                                     ~43, ->69
   41    67    >   ECHO                                                     'str_starts_with+is+faster%0A'
   40    68      > JMP                                                      ->70
   43    69    >   ECHO                                                     'strpos+is+faster%0A'
   44    70    > > RETURN                                                   1


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

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
144.76 ms | 1438 KiB | 17 Q