3v4l.org

run code in 300+ PHP versions simultaneously
<?php // The slow, although default way of doing fibonacci --------------------------- function _fibs($n) { if ($n < 2) return $n; else return _fibs($n-1) + _fibs($n-2); } function fibs() { $i = 0; while (1) yield _fibs($i++); } // The speedy way to do fibonacci----------------------------------------------- function fibf() { $a = 0; $b = 0; $t = 1; while (1) { yield $b; if ($b > 0) { $t = $a; $a = $b; } $b += $t; } } // Test utils ------------------------------------------------------------------ // // function to test an iterator by iterating it until the $compare function // returns false // // print the result visually because its sexy function visual_iterator_tester($fn, $cmpr) { echo "timing $fn "; $then = time(); foreach ($fn() as $f) { if ($cmpr($f)) break; echo "."; } $now = time(); $diff = $now - $then; echo "\n$fn completed in $diff second(s)\n"; } // Run the damn thing ---------------------------------------------------------- $fns = ['fibs', 'fibf']; $runs = 9999999; foreach ($fns as $fn) visual_iterator_tester($fn, function($a) use($runs) { return $a > $runs; });
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 11
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/h9nA3
function name:  (null)
number of ops:  13
compiled vars:  !0 = $fns, !1 = $runs, !2 = $fn
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   65     0  E >   ASSIGN                                                   !0, <array>
   66     1        ASSIGN                                                   !1, 9999999
   68     2      > FE_RESET_R                                       $5      !0, ->11
          3    > > FE_FETCH_R                                               $5, !2, ->11
   69     4    >   INIT_FCALL                                               'visual_iterator_tester'
          5        SEND_VAR                                                 !2
          6        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fh9nA3%3A69%240'
          7        BIND_LEXICAL                                             ~6, !1
          8        SEND_VAL                                                 ~6
          9        DO_FCALL                                      0          
   68    10      > JMP                                                      ->3
         11    >   FE_FREE                                                  $5
   69    12      > RETURN                                                   1

Function _fibs:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/h9nA3
function name:  _fibs
number of ops:  16
compiled vars:  !0 = $n
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
    7     1        IS_SMALLER                                               !0, 2
          2      > JMPZ                                                     ~1, ->5
    8     3    > > RETURN                                                   !0
          4*       JMP                                                      ->15
   10     5    >   INIT_FCALL_BY_NAME                                       '_fibs'
          6        SUB                                              ~2      !0, 1
          7        SEND_VAL_EX                                              ~2
          8        DO_FCALL                                      0  $3      
          9        INIT_FCALL_BY_NAME                                       '_fibs'
         10        SUB                                              ~4      !0, 2
         11        SEND_VAL_EX                                              ~4
         12        DO_FCALL                                      0  $5      
         13        ADD                                              ~6      $3, $5
         14      > RETURN                                                   ~6
   11    15*     > RETURN                                                   null

End of function _fibs

Function fibs:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 9, Position 2 = 3
Branch analysis from position: 9
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 9, Position 2 = 3
Branch analysis from position: 9
Branch analysis from position: 3
filename:       /in/h9nA3
function name:  fibs
number of ops:  10
compiled vars:  !0 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   GENERATOR_CREATE                                         
   15     1        ASSIGN                                                   !0, 0
   16     2      > JMP                                                      ->8
   17     3    >   INIT_FCALL                                               '_fibs'
          4        POST_INC                                         ~2      !0
          5        SEND_VAL                                                 ~2
          6        DO_FCALL                                      0  $3      
          7        YIELD                                                    $3
   16     8    > > JMPNZ                                                    1, ->3
   18     9    > > GENERATOR_RETURN                                         

End of function fibs

Function fibf:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 12, Position 2 = 5
Branch analysis from position: 12
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 12, Position 2 = 5
Branch analysis from position: 12
Branch analysis from position: 5
Branch analysis from position: 10
filename:       /in/h9nA3
function name:  fibf
number of ops:  13
compiled vars:  !0 = $a, !1 = $b, !2 = $t
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   GENERATOR_CREATE                                         
   24     1        ASSIGN                                                   !0, 0
   25     2        ASSIGN                                                   !1, 0
   26     3        ASSIGN                                                   !2, 1
   27     4      > JMP                                                      ->11
   29     5    >   YIELD                                                    !1
   30     6        IS_SMALLER                                               0, !1
          7      > JMPZ                                                     ~7, ->10
   31     8    >   ASSIGN                                                   !2, !0
   32     9        ASSIGN                                                   !0, !1
   34    10    >   ASSIGN_OP                                     1          !1, !2
   27    11    > > JMPNZ                                                    1, ->5
   36    12    > > GENERATOR_RETURN                                         

End of function fibf

Function visual_iterator_tester:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 20
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 20
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 18
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 20
Branch analysis from position: 20
filename:       /in/h9nA3
function name:  visual_iterator_tester
number of ops:  33
compiled vars:  !0 = $fn, !1 = $cmpr, !2 = $then, !3 = $f, !4 = $now, !5 = $diff
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   47     2        ROPE_INIT                                     3  ~7      'timing+'
          3        ROPE_ADD                                      1  ~7      ~7, !0
          4        ROPE_END                                      2  ~6      ~7, '+'
          5        ECHO                                                     ~6
   49     6        INIT_FCALL                                               'time'
          7        DO_ICALL                                         $9      
          8        ASSIGN                                                   !2, $9
   51     9        INIT_DYNAMIC_CALL                                        !0
         10        DO_FCALL                                      0  $11     
         11      > FE_RESET_R                                       $12     $11, ->20
         12    > > FE_FETCH_R                                               $12, !3, ->20
   53    13    >   INIT_DYNAMIC_CALL                                        !1
         14        SEND_VAR_EX                                              !3
         15        DO_FCALL                                      0  $13     
         16      > JMPZ                                                     $13, ->18
         17    > > JMP                                                      ->20
   54    18    >   ECHO                                                     '.'
   51    19      > JMP                                                      ->12
         20    >   FE_FREE                                                  $12
   57    21        INIT_FCALL                                               'time'
         22        DO_ICALL                                         $14     
         23        ASSIGN                                                   !4, $14
   58    24        SUB                                              ~16     !4, !2
         25        ASSIGN                                                   !5, ~16
   60    26        ROPE_INIT                                     5  ~19     '%0A'
         27        ROPE_ADD                                      1  ~19     ~19, !0
         28        ROPE_ADD                                      2  ~19     ~19, '+completed+in+'
         29        ROPE_ADD                                      3  ~19     ~19, !5
         30        ROPE_END                                      4  ~18     ~19, '+second%28s%29%0A'
         31        ECHO                                                     ~18
   61    32      > RETURN                                                   null

End of function visual_iterator_tester

Function %00%7Bclosure%7D%2Fin%2Fh9nA3%3A69%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/h9nA3
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $a, !1 = $runs
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   69     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        IS_SMALLER                                       ~2      !1, !0
          3      > RETURN                                                   ~2
          4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fh9nA3%3A69%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.62 ms | 1407 KiB | 17 Q