3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Fibonacci number creator. * * @param int $grandparent * @param int $parent * @return int */ function f(int $grandparent, int $parent): int { return intval($parent + $grandparent); } /** * A classical fibonacci range builder. * * @param int $length * @return int[] */ function builder(int $length): array { $grandparent = 0; $parent = 1; $range = [$grandparent, $parent]; for ($i = 0; $i <= $length; $i++) { $child = f($grandparent, $parent); $grandparent = $parent; $parent = $child; $range[] = $child; } return $range; } /** * Generator for fibonacci numbers. * * @param int $length * * @yields int */ function generator(int $length) { $grandparent = 0; $parent = 1; for ($i = 0; $i <= $length; $i++) { yield $child = f($grandparent, $parent); $grandparent = $parent; $parent = $child; } } $maxNumIterations = 999999; for ($numIterations = 0; $numIterations < $maxNumIterations; $numIterations++) { gc_collect_cycles(); $builderUsage = 0; foreach (builder($numIterations) as $fibonacciNumber) { $currentUsage = memory_get_usage(false); if ($currentUsage > $builderUsage) { $builderUsage = $currentUsage; } } gc_collect_cycles(); $generatorUsage = 0; foreach (generator($numIterations) as $fibonacciNumber) { $currentUsage = memory_get_usage(false); if ($currentUsage > $generatorUsage) { $generatorUsage = $currentUsage; } } echo sprintf( "#%d\tBuilder: %.2f kb\tGenerator: %.2f kb\t%d", $numIterations, $builderUsage / 1024, $generatorUsage / 1024, $fibonacciNumber ) . PHP_EOL; if ($builderUsage > $generatorUsage) { echo sprintf( 'Found difference at iteration #%d and fibonacci number %d', $numIterations, $fibonacciNumber ) . PHP_EOL; break; } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
2 jumps found. (Code = 44) Position 1 = 61, Position 2 = 3
Branch analysis from position: 61
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 19
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 19
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 18
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 18
Branch analysis from position: 19
2 jumps found. (Code = 77) Position 1 = 27, Position 2 = 36
Branch analysis from position: 27
2 jumps found. (Code = 78) Position 1 = 28, Position 2 = 36
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 35
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 35
Branch analysis from position: 36
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 58
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 61
Branch analysis from position: 61
Branch analysis from position: 58
2 jumps found. (Code = 44) Position 1 = 61, Position 2 = 3
Branch analysis from position: 61
Branch analysis from position: 3
Branch analysis from position: 36
Branch analysis from position: 19
filename:       /in/C4IlF
function name:  (null)
number of ops:  62
compiled vars:  !0 = $maxNumIterations, !1 = $numIterations, !2 = $builderUsage, !3 = $fibonacciNumber, !4 = $currentUsage, !5 = $generatorUsage
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   ASSIGN                                                   !0, 999999
   57     1        ASSIGN                                                   !1, 0
          2      > JMP                                                      ->59
   58     3    >   INIT_FCALL                                               'gc_collect_cycles'
          4        DO_ICALL                                                 
   59     5        ASSIGN                                                   !2, 0
   61     6        INIT_FCALL                                               'builder'
          7        SEND_VAR                                                 !1
          8        DO_FCALL                                      0  $10     
          9      > FE_RESET_R                                       $11     $10, ->19
         10    > > FE_FETCH_R                                               $11, !3, ->19
   62    11    >   INIT_FCALL                                               'memory_get_usage'
         12        SEND_VAL                                                 <false>
         13        DO_ICALL                                         $12     
         14        ASSIGN                                                   !4, $12
   64    15        IS_SMALLER                                               !2, !4
         16      > JMPZ                                                     ~14, ->18
   65    17    >   ASSIGN                                                   !2, !4
   61    18    > > JMP                                                      ->10
         19    >   FE_FREE                                                  $11
   69    20        INIT_FCALL                                               'gc_collect_cycles'
         21        DO_ICALL                                                 
   70    22        ASSIGN                                                   !5, 0
   72    23        INIT_FCALL                                               'generator'
         24        SEND_VAR                                                 !1
         25        DO_FCALL                                      0  $18     
         26      > FE_RESET_R                                       $19     $18, ->36
         27    > > FE_FETCH_R                                               $19, !3, ->36
   73    28    >   INIT_FCALL                                               'memory_get_usage'
         29        SEND_VAL                                                 <false>
         30        DO_ICALL                                         $20     
         31        ASSIGN                                                   !4, $20
   75    32        IS_SMALLER                                               !5, !4
         33      > JMPZ                                                     ~22, ->35
   76    34    >   ASSIGN                                                   !5, !4
   72    35    > > JMP                                                      ->27
         36    >   FE_FREE                                                  $19
   80    37        INIT_FCALL                                               'sprintf'
   81    38        SEND_VAL                                                 '%23%25d%09Builder%3A+%25.2f+kb%09Generator%3A+%25.2f+kb%09%25d'
   82    39        SEND_VAR                                                 !1
   83    40        DIV                                              ~24     !2, 1024
         41        SEND_VAL                                                 ~24
   84    42        DIV                                              ~25     !5, 1024
         43        SEND_VAL                                                 ~25
   85    44        SEND_VAR                                                 !3
   80    45        DO_ICALL                                         $26     
   86    46        CONCAT                                           ~27     $26, '%0A'
         47        ECHO                                                     ~27
   88    48        IS_SMALLER                                               !5, !2
         49      > JMPZ                                                     ~28, ->58
   89    50    >   INIT_FCALL                                               'sprintf'
   90    51        SEND_VAL                                                 'Found+difference+at+iteration+%23%25d+and+fibonacci+number+%25d'
   91    52        SEND_VAR                                                 !1
   92    53        SEND_VAR                                                 !3
   89    54        DO_ICALL                                         $29     
   93    55        CONCAT                                           ~30     $29, '%0A'
         56        ECHO                                                     ~30
   94    57      > JMP                                                      ->61
   57    58    >   PRE_INC                                                  !1
         59    >   IS_SMALLER                                               !1, !0
         60      > JMPNZ                                                    ~32, ->3
   96    61    > > RETURN                                                   1

Function f:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/C4IlF
function name:  f
number of ops:  8
compiled vars:  !0 = $grandparent, !1 = $parent
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   11     2        ADD                                              ~2      !1, !0
          3        CAST                                          4  ~3      ~2
          4        VERIFY_RETURN_TYPE                                       ~3
          5      > RETURN                                                   ~3
   12     6*       VERIFY_RETURN_TYPE                                       
          7*     > RETURN                                                   null

End of function f

Function builder:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 8
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 8
Branch analysis from position: 20
Branch analysis from position: 8
filename:       /in/C4IlF
function name:  builder
number of ops:  24
compiled vars:  !0 = $length, !1 = $grandparent, !2 = $parent, !3 = $range, !4 = $i, !5 = $child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
   22     1        ASSIGN                                                   !1, 0
   23     2        ASSIGN                                                   !2, 1
   24     3        INIT_ARRAY                                       ~8      !1
          4        ADD_ARRAY_ELEMENT                                ~8      !2
          5        ASSIGN                                                   !3, ~8
   26     6        ASSIGN                                                   !4, 0
          7      > JMP                                                      ->18
   27     8    >   INIT_FCALL                                               'f'
          9        SEND_VAR                                                 !1
         10        SEND_VAR                                                 !2
         11        DO_FCALL                                      0  $11     
         12        ASSIGN                                                   !5, $11
   28    13        ASSIGN                                                   !1, !2
   29    14        ASSIGN                                                   !2, !5
   30    15        ASSIGN_DIM                                               !3
         16        OP_DATA                                                  !5
   26    17        PRE_INC                                                  !4
         18    >   IS_SMALLER_OR_EQUAL                                      !4, !0
         19      > JMPNZ                                                    ~17, ->8
   33    20    >   VERIFY_RETURN_TYPE                                       !3
         21      > RETURN                                                   !3
   34    22*       VERIFY_RETURN_TYPE                                       
         23*     > RETURN                                                   null

End of function builder

Function generator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 6
Branch analysis from position: 17
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 6
Branch analysis from position: 17
Branch analysis from position: 6
filename:       /in/C4IlF
function name:  generator
number of ops:  18
compiled vars:  !0 = $length, !1 = $grandparent, !2 = $parent, !3 = $i, !4 = $child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
          1        GENERATOR_CREATE                                         
   45     2        ASSIGN                                                   !1, 0
   46     3        ASSIGN                                                   !2, 1
   48     4        ASSIGN                                                   !3, 0
          5      > JMP                                                      ->15
   49     6    >   INIT_FCALL                                               'f'
          7        SEND_VAR                                                 !1
          8        SEND_VAR                                                 !2
          9        DO_FCALL                                      0  $8      
         10        ASSIGN                                           ~9      !4, $8
         11        YIELD                                                    ~9
   50    12        ASSIGN                                                   !1, !2
   51    13        ASSIGN                                                   !2, !4
   48    14        PRE_INC                                                  !3
         15    >   IS_SMALLER_OR_EQUAL                                      !3, !0
         16      > JMPNZ                                                    ~14, ->6
   53    17    > > GENERATOR_RETURN                                         

End of function generator

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.96 ms | 1022 KiB | 20 Q