3v4l.org

run code in 300+ PHP versions simultaneously
<?php function strpose_simple($haystack, $needle, $index = 0) { $hlen = strlen($haystack); $nlen = strlen($needle); do { $pos = strpos($haystack, $needle, $index); if ($pos !== false) { $end = $pos+$nlen; if (!isset($haystack[$pos-1]) || !trim($haystack[$pos-1])) { if (!isset($haystack[$end]) || !trim($haystack[$end])) { return $pos; } else $index = $pos+1; } else $index = $pos+1; } } while ($pos !== false && $pos < $hlen); return $pos; } function strpose_slow($haystack, $needle, $offset = 0) { $found = preg_match( '(\b(?:'.preg_quote($needle).')\b)Su', $haystack, $match, PREG_OFFSET_CAPTURE, $offset ); return ($found) ? $match[0][1] : FALSE; } $haystack = "Hello World"; $needles = array( "ll", "Hello", "Worlds" ); foreach (array('strpose_simple', 'strpose_slow') as $function) { $begin = 0; $end = 100000; $start = microtime(true); while ($begin++<$end) { foreach ($needles as $id => $needle) { $function($haystack, $needle); } } printf("%s: %.4f seconds/%d peak\n", $function, microtime(true)-$start, memory_get_usage(true)); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 37
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 37
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 11
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 19
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 19
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 11
Branch analysis from position: 23
Branch analysis from position: 11
Branch analysis from position: 19
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
filename:       /in/LtQ79
function name:  (null)
number of ops:  39
compiled vars:  !0 = $haystack, !1 = $needles, !2 = $function, !3 = $begin, !4 = $end, !5 = $start, !6 = $needle, !7 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   ASSIGN                                                   !0, 'Hello+World'
   37     1        ASSIGN                                                   !1, <array>
   41     2      > FE_RESET_R                                       $10     <array>, ->37
          3    > > FE_FETCH_R                                               $10, !2, ->37
   42     4    >   ASSIGN                                                   !3, 0
   43     5        ASSIGN                                                   !4, 100000
   44     6        INIT_FCALL                                               'microtime'
          7        SEND_VAL                                                 <true>
          8        DO_ICALL                                         $13     
          9        ASSIGN                                                   !5, $13
   45    10      > JMP                                                      ->20
   46    11    > > FE_RESET_R                                       $15     !1, ->19
         12    > > FE_FETCH_R                                       ~16     $15, !6, ->19
         13    >   ASSIGN                                                   !7, ~16
   47    14        INIT_DYNAMIC_CALL                                        !2
         15        SEND_VAR_EX                                              !0
         16        SEND_VAR_EX                                              !6
         17        DO_FCALL                                      0          
   46    18      > JMP                                                      ->12
         19    >   FE_FREE                                                  $15
   45    20    >   POST_INC                                         ~19     !3
         21        IS_SMALLER                                               ~19, !4
         22      > JMPNZ                                                    ~20, ->11
   50    23    >   INIT_FCALL                                               'printf'
         24        SEND_VAL                                                 '%25s%3A+%25.4f+seconds%2F%25d+peak%0A'
         25        SEND_VAR                                                 !2
         26        INIT_FCALL                                               'microtime'
         27        SEND_VAL                                                 <true>
         28        DO_ICALL                                         $21     
         29        SUB                                              ~22     $21, !5
         30        SEND_VAL                                                 ~22
         31        INIT_FCALL                                               'memory_get_usage'
         32        SEND_VAL                                                 <true>
         33        DO_ICALL                                         $23     
         34        SEND_VAR                                                 $23
         35        DO_ICALL                                                 
   41    36      > JMP                                                      ->3
         37    >   FE_FREE                                                  $10
   51    38      > RETURN                                                   1

Function strpose_simple:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 46
Branch analysis from position: 15
2 jumps found. (Code = 47) Position 1 = 21, Position 2 = 28
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 44
Branch analysis from position: 29
2 jumps found. (Code = 47) Position 1 = 32, Position 2 = 38
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 41
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
2 jumps found. (Code = 46) Position 1 = 48, Position 2 = 50
Branch analysis from position: 48
2 jumps found. (Code = 44) Position 1 = 51, Position 2 = 7
Branch analysis from position: 51
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
Branch analysis from position: 50
Branch analysis from position: 38
Branch analysis from position: 44
2 jumps found. (Code = 46) Position 1 = 48, Position 2 = 50
Branch analysis from position: 48
Branch analysis from position: 50
Branch analysis from position: 28
Branch analysis from position: 46
filename:       /in/LtQ79
function name:  strpose_simple
number of ops:  53
compiled vars:  !0 = $haystack, !1 = $needle, !2 = $index, !3 = $hlen, !4 = $nlen, !5 = $pos, !6 = $end
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      0
    4     3        STRLEN                                           ~7      !0
          4        ASSIGN                                                   !3, ~7
    5     5        STRLEN                                           ~9      !1
          6        ASSIGN                                                   !4, ~9
    8     7    >   INIT_FCALL                                               'strpos'
          8        SEND_VAR                                                 !0
          9        SEND_VAR                                                 !1
         10        SEND_VAR                                                 !2
         11        DO_ICALL                                         $11     
         12        ASSIGN                                                   !5, $11
    9    13        TYPE_CHECK                                  1018          !5
         14      > JMPZ                                                     ~13, ->46
   10    15    >   ADD                                              ~14     !5, !4
         16        ASSIGN                                                   !6, ~14
   12    17        SUB                                              ~16     !5, 1
         18        ISSET_ISEMPTY_DIM_OBJ                         0  ~17     !0, ~16
         19        BOOL_NOT                                         ~18     ~17
         20      > JMPNZ_EX                                         ~18     ~18, ->28
         21    >   INIT_FCALL                                               'trim'
         22        SUB                                              ~19     !5, 1
         23        FETCH_DIM_R                                      ~20     !0, ~19
         24        SEND_VAL                                                 ~20
         25        DO_ICALL                                         $21     
         26        BOOL_NOT                                         ~22     $21
         27        BOOL                                             ~18     ~22
         28    > > JMPZ                                                     ~18, ->44
   13    29    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~23     !0, !6
         30        BOOL_NOT                                         ~24     ~23
         31      > JMPNZ_EX                                         ~24     ~24, ->38
         32    >   INIT_FCALL                                               'trim'
         33        FETCH_DIM_R                                      ~25     !0, !6
         34        SEND_VAL                                                 ~25
         35        DO_ICALL                                         $26     
         36        BOOL_NOT                                         ~27     $26
         37        BOOL                                             ~24     ~27
         38    > > JMPZ                                                     ~24, ->41
   14    39    > > RETURN                                                   !5
         40*       JMP                                                      ->43
   15    41    >   ADD                                              ~28     !5, 1
         42        ASSIGN                                                   !2, ~28
         43      > JMP                                                      ->46
   16    44    >   ADD                                              ~30     !5, 1
         45        ASSIGN                                                   !2, ~30
   19    46    >   TYPE_CHECK                                  1018  ~32     !5
         47      > JMPZ_EX                                          ~32     ~32, ->50
         48    >   IS_SMALLER                                       ~33     !5, !3
         49        BOOL                                             ~32     ~33
         50    > > JMPNZ                                                    ~32, ->7
   21    51    > > RETURN                                                   !5
   22    52*     > RETURN                                                   null

End of function strpose_simple

Function strpose_slow:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 21
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LtQ79
function name:  strpose_slow
number of ops:  24
compiled vars:  !0 = $haystack, !1 = $needle, !2 = $offset, !3 = $found, !4 = $match
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      0
   25     3        INIT_FCALL                                               'preg_match'
   26     4        INIT_FCALL                                               'preg_quote'
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $5      
          7        CONCAT                                           ~6      '%28%5Cb%28%3F%3A', $5
          8        CONCAT                                           ~7      ~6, '%29%5Cb%29Su'
          9        SEND_VAL                                                 ~7
   27    10        SEND_VAR                                                 !0
   28    11        SEND_REF                                                 !4
   29    12        SEND_VAL                                                 256
   30    13        SEND_VAR                                                 !2
         14        DO_ICALL                                         $8      
   25    15        ASSIGN                                                   !3, $8
   32    16      > JMPZ                                                     !3, ->21
         17    >   FETCH_DIM_R                                      ~10     !4, 0
         18        FETCH_DIM_R                                      ~11     ~10, 1
         19        QM_ASSIGN                                        ~12     ~11
         20      > JMP                                                      ->22
         21    >   QM_ASSIGN                                        ~12     <false>
         22    > > RETURN                                                   ~12
   33    23*     > RETURN                                                   null

End of function strpose_slow

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.37 ms | 1408 KiB | 27 Q