3v4l.org

run code in 300+ PHP versions simultaneously
<?php function wordsToArr2($str) { $words = [$str]; while ($pos =strpos ( $str , ' ')) { $str=substr($str,($pos+1)); $words[] =$str; } return $words; } function wordsToArr3($str) { $base = array_reverse(explode(' ', $str)); $words = [$placeholder = array_shift($base)]; foreach ($base as $word) { $words[] = $placeholder = $word . ' ' . $placeholder; } return array_reverse($words); } function wordsToArr4($str) { $base = explode(' ', $str); end($base); $l = key($base); $words = [$placeholder = $base[$l--]]; for ($i=$l; $i>=0;$i--) { $words[] = $placeholder = $base[$i] . ' ' . $placeholder; } return array_reverse($words); } function wordsToArr5($str) { $base = str_word_count($str, 1); $l = count($base)-1; $words = [$placeholder = $base[$l--]]; for ($i=$l; $i>=0;$i--) { $words[] = $placeholder = $base[$i] . ' ' . $placeholder; } return array_reverse($words); } function bench($callback, $arg, $runs = 100) { $t = 0; $now = microtime(true); for ($i=0; $i<$runs; $i++) { $callback($arg); } $t += (microtime(true) - $now); return [number_format($t, 8), $i]; } $sentence = 'The quick brown fox jumps over the lazy dog'; list($t, $i) = bench('wordsToArr2', $sentence); echo 'strpos '. $i .' times in: ' . $t . '/sec' . PHP_EOL; list($t, $i) = bench('wordsToArr3', $sentence); echo 'concat '. $i .' times in: ' . $t . '/sec' . PHP_EOL; list($t, $i) = bench('wordsToArr4', $sentence); echo 'end/key '. $i .' times in: ' . $t . '/sec' . PHP_EOL; list($t, $i) = bench('wordsToArr5', $sentence); echo 'count-- '. $i .' times in: ' . $t . '/sec' . PHP_EOL;
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VfJku
function name:  (null)
number of ops:  62
compiled vars:  !0 = $sentence, !1 = $t, !2 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   ASSIGN                                                   !0, 'The+quick+brown+fox+jumps+over+the+lazy+dog'
   59     1        INIT_FCALL                                               'bench'
          2        SEND_VAL                                                 'wordsToArr2'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $4      
          5        FETCH_LIST_R                                     $5      $4, 0
          6        ASSIGN                                                   !1, $5
          7        FETCH_LIST_R                                     $7      $4, 1
          8        ASSIGN                                                   !2, $7
          9        FREE                                                     $4
   60    10        CONCAT                                           ~9      'strpos+', !2
         11        CONCAT                                           ~10     ~9, '+times+in%3A+'
         12        CONCAT                                           ~11     ~10, !1
         13        CONCAT                                           ~12     ~11, '%2Fsec'
         14        CONCAT                                           ~13     ~12, '%0A'
         15        ECHO                                                     ~13
   63    16        INIT_FCALL                                               'bench'
         17        SEND_VAL                                                 'wordsToArr3'
         18        SEND_VAR                                                 !0
         19        DO_FCALL                                      0  $14     
         20        FETCH_LIST_R                                     $15     $14, 0
         21        ASSIGN                                                   !1, $15
         22        FETCH_LIST_R                                     $17     $14, 1
         23        ASSIGN                                                   !2, $17
         24        FREE                                                     $14
   64    25        CONCAT                                           ~19     'concat+', !2
         26        CONCAT                                           ~20     ~19, '+times+in%3A+'
         27        CONCAT                                           ~21     ~20, !1
         28        CONCAT                                           ~22     ~21, '%2Fsec'
         29        CONCAT                                           ~23     ~22, '%0A'
         30        ECHO                                                     ~23
   66    31        INIT_FCALL                                               'bench'
         32        SEND_VAL                                                 'wordsToArr4'
         33        SEND_VAR                                                 !0
         34        DO_FCALL                                      0  $24     
         35        FETCH_LIST_R                                     $25     $24, 0
         36        ASSIGN                                                   !1, $25
         37        FETCH_LIST_R                                     $27     $24, 1
         38        ASSIGN                                                   !2, $27
         39        FREE                                                     $24
   67    40        CONCAT                                           ~29     'end%2Fkey+', !2
         41        CONCAT                                           ~30     ~29, '+times+in%3A+'
         42        CONCAT                                           ~31     ~30, !1
         43        CONCAT                                           ~32     ~31, '%2Fsec'
         44        CONCAT                                           ~33     ~32, '%0A'
         45        ECHO                                                     ~33
   69    46        INIT_FCALL                                               'bench'
         47        SEND_VAL                                                 'wordsToArr5'
         48        SEND_VAR                                                 !0
         49        DO_FCALL                                      0  $34     
         50        FETCH_LIST_R                                     $35     $34, 0
         51        ASSIGN                                                   !1, $35
         52        FETCH_LIST_R                                     $37     $34, 1
         53        ASSIGN                                                   !2, $37
         54        FREE                                                     $34
   70    55        CONCAT                                           ~39     'count--+', !2
         56        CONCAT                                           ~40     ~39, '+times+in%3A+'
         57        CONCAT                                           ~41     ~40, !1
         58        CONCAT                                           ~42     ~41, '%2Fsec'
         59        CONCAT                                           ~43     ~42, '%0A'
         60        ECHO                                                     ~43
   71    61      > RETURN                                                   1

Function wordstoarr2:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 4
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 4
Branch analysis from position: 18
Branch analysis from position: 4
filename:       /in/VfJku
function name:  wordsToArr2
number of ops:  20
compiled vars:  !0 = $str, !1 = $words, !2 = $pos
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        INIT_ARRAY                                       ~3      !0
          2        ASSIGN                                                   !1, ~3
    5     3      > JMP                                                      ->12
    6     4    >   INIT_FCALL                                               'substr'
          5        SEND_VAR                                                 !0
          6        ADD                                              ~5      !2, 1
          7        SEND_VAL                                                 ~5
          8        DO_ICALL                                         $6      
          9        ASSIGN                                                   !0, $6
    7    10        ASSIGN_DIM                                               !1
         11        OP_DATA                                                  !0
    5    12    >   INIT_FCALL                                               'strpos'
         13        SEND_VAR                                                 !0
         14        SEND_VAL                                                 '+'
         15        DO_ICALL                                         $9      
         16        ASSIGN                                           ~10     !2, $9
         17      > JMPNZ                                                    ~10, ->4
   10    18    > > RETURN                                                   !1
   11    19*     > RETURN                                                   null

End of function wordstoarr2

Function wordstoarr3:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 23
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 23
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
filename:       /in/VfJku
function name:  wordsToArr3
number of ops:  29
compiled vars:  !0 = $str, !1 = $base, !2 = $words, !3 = $placeholder, !4 = $word
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   14     1        INIT_FCALL                                               'array_reverse'
          2        INIT_FCALL                                               'explode'
          3        SEND_VAL                                                 '+'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $5      
          6        SEND_VAR                                                 $5
          7        DO_ICALL                                         $6      
          8        ASSIGN                                                   !1, $6
   15     9        INIT_FCALL                                               'array_shift'
         10        SEND_REF                                                 !1
         11        DO_ICALL                                         $8      
         12        ASSIGN                                           ~9      !3, $8
         13        INIT_ARRAY                                       ~10     ~9
         14        ASSIGN                                                   !2, ~10
   16    15      > FE_RESET_R                                       $12     !1, ->23
         16    > > FE_FETCH_R                                               $12, !4, ->23
   17    17    >   CONCAT                                           ~14     !4, '+'
         18        CONCAT                                           ~15     ~14, !3
         19        ASSIGN                                           ~16     !3, ~15
         20        ASSIGN_DIM                                               !2
         21        OP_DATA                                                  ~16
   16    22      > JMP                                                      ->16
         23    >   FE_FREE                                                  $12
   20    24        INIT_FCALL                                               'array_reverse'
         25        SEND_VAR                                                 !2
         26        DO_ICALL                                         $17     
         27      > RETURN                                                   $17
   21    28*     > RETURN                                                   null

End of function wordstoarr3

Function wordstoarr4:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 20
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 20
Branch analysis from position: 29
Branch analysis from position: 20
filename:       /in/VfJku
function name:  wordsToArr4
number of ops:  34
compiled vars:  !0 = $str, !1 = $base, !2 = $l, !3 = $words, !4 = $placeholder, !5 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
   24     1        INIT_FCALL                                               'explode'
          2        SEND_VAL                                                 '+'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $6      
          5        ASSIGN                                                   !1, $6
   25     6        INIT_FCALL                                               'end'
          7        SEND_REF                                                 !1
          8        DO_ICALL                                                 
   26     9        INIT_FCALL                                               'key'
         10        SEND_VAR                                                 !1
         11        DO_ICALL                                         $9      
         12        ASSIGN                                                   !2, $9
   27    13        POST_DEC                                         ~11     !2
         14        FETCH_DIM_R                                      ~12     !1, ~11
         15        ASSIGN                                           ~13     !4, ~12
         16        INIT_ARRAY                                       ~14     ~13
         17        ASSIGN                                                   !3, ~14
   28    18        ASSIGN                                                   !5, !2
         19      > JMP                                                      ->27
   29    20    >   FETCH_DIM_R                                      ~18     !1, !5
         21        CONCAT                                           ~19     ~18, '+'
         22        CONCAT                                           ~20     ~19, !4
         23        ASSIGN                                           ~21     !4, ~20
         24        ASSIGN_DIM                                               !3
         25        OP_DATA                                                  ~21
   28    26        PRE_DEC                                                  !5
         27    >   IS_SMALLER_OR_EQUAL                                      0, !5
         28      > JMPNZ                                                    ~23, ->20
   32    29    >   INIT_FCALL                                               'array_reverse'
         30        SEND_VAR                                                 !3
         31        DO_ICALL                                         $24     
         32      > RETURN                                                   $24
   33    33*     > RETURN                                                   null

End of function wordstoarr4

Function wordstoarr5:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 16
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 16
Branch analysis from position: 25
Branch analysis from position: 16
filename:       /in/VfJku
function name:  wordsToArr5
number of ops:  30
compiled vars:  !0 = $str, !1 = $base, !2 = $l, !3 = $words, !4 = $placeholder, !5 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
   36     1        INIT_FCALL                                               'str_word_count'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 1
          4        DO_ICALL                                         $6      
          5        ASSIGN                                                   !1, $6
   37     6        COUNT                                            ~8      !1
          7        SUB                                              ~9      ~8, 1
          8        ASSIGN                                                   !2, ~9
   38     9        POST_DEC                                         ~11     !2
         10        FETCH_DIM_R                                      ~12     !1, ~11
         11        ASSIGN                                           ~13     !4, ~12
         12        INIT_ARRAY                                       ~14     ~13
         13        ASSIGN                                                   !3, ~14
   39    14        ASSIGN                                                   !5, !2
         15      > JMP                                                      ->23
   40    16    >   FETCH_DIM_R                                      ~18     !1, !5
         17        CONCAT                                           ~19     ~18, '+'
         18        CONCAT                                           ~20     ~19, !4
         19        ASSIGN                                           ~21     !4, ~20
         20        ASSIGN_DIM                                               !3
         21        OP_DATA                                                  ~21
   39    22        PRE_DEC                                                  !5
         23    >   IS_SMALLER_OR_EQUAL                                      0, !5
         24      > JMPNZ                                                    ~23, ->16
   43    25    >   INIT_FCALL                                               'array_reverse'
         26        SEND_VAR                                                 !3
         27        DO_ICALL                                         $24     
         28      > RETURN                                                   $24
   44    29*     > RETURN                                                   null

End of function wordstoarr5

Function bench:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 10
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 16, Position 2 = 10
Branch analysis from position: 16
Branch analysis from position: 10
filename:       /in/VfJku
function name:  bench
number of ops:  29
compiled vars:  !0 = $callback, !1 = $arg, !2 = $runs, !3 = $t, !4 = $now, !5 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      100
   47     3        ASSIGN                                                   !3, 0
   48     4        INIT_FCALL                                               'microtime'
          5        SEND_VAL                                                 <true>
          6        DO_ICALL                                         $7      
          7        ASSIGN                                                   !4, $7
   49     8        ASSIGN                                                   !5, 0
          9      > JMP                                                      ->14
   50    10    >   INIT_DYNAMIC_CALL                                        !0
         11        SEND_VAR_EX                                              !1
         12        DO_FCALL                                      0          
   49    13        PRE_INC                                                  !5
         14    >   IS_SMALLER                                               !5, !2
         15      > JMPNZ                                                    ~12, ->10
   52    16    >   INIT_FCALL                                               'microtime'
         17        SEND_VAL                                                 <true>
         18        DO_ICALL                                         $13     
         19        SUB                                              ~14     $13, !4
         20        ASSIGN_OP                                     1          !3, ~14
   54    21        INIT_FCALL                                               'number_format'
         22        SEND_VAR                                                 !3
         23        SEND_VAL                                                 8
         24        DO_ICALL                                         $16     
         25        INIT_ARRAY                                       ~17     $16
         26        ADD_ARRAY_ELEMENT                                ~17     !5
         27      > RETURN                                                   ~17
   55    28*     > RETURN                                                   null

End of function bench

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
176.92 ms | 1044 KiB | 27 Q