3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Various methods of computing Fibonacci numbers in PHP */ class Fibonacci { /** * @var array Memoization cache * @see Fibonacci::memoized */ protected $cache = array(0 => 0, 1 => 1); /** * Fibonacci using recursion */ public function recursive($n) { if ($n == 0) { return 0; } if ($n == 1) { return 1; } return $this->recursive($n - 1) + $this->recursive($n - 2); } /** * Fibonacci using an iterative approach */ public function iterative($n) { $a = 0; $b = 1; for ($i = 0; $i < $n; $i++) { $c = $a; $a = $b; $b += $c; } return $a; } /** * Fibonacci using Binet's formula * @link http://mathworld.wolfram.com/BinetsFibonacciNumberFormula.html */ public function binet($n) { $phi = (1 + sqrt(5)) / 2; return (pow($phi, $n) - pow(1 - $phi, $n)) / sqrt(5); } /** * Fibonacci using a cache */ public function memoized($n) { if (!isset($this->cache[$n])) { $this->cache[$n] = $this->memoized($n - 1) + $this->memoized($n - 2); } return $this->cache[$n]; } } /** * Test each Fibonacci method and output the speed */ function test($total, $callback) { echo $callback[1] . ":\n"; $t = microtime(true); for ($x = 0; $x < $total; $x++) { call_user_func($callback, $x); } $finish = microtime(true) - $t; echo ($finish * 1000) . ' ms (' . (($finish / $total) * 1000) . ")\n\n"; } // You can pass in the total number of sequences to calculate as a CLI argument $total = isset($argv[1]) ? $argv[1] : 25; $fib = new Fibonacci(); test($total, array($fib, 'iterative')); test($total, array($fib, 'binet')); test($total, array($fib, 'memoized')); // Limit our attempts with recursion if ($total <= 30) { test($total, array($fib, 'recursive')); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 5
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 36
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 36
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 36
Branch analysis from position: 30
Branch analysis from position: 36
filename:       /in/5ksoN
function name:  (null)
number of ops:  37
compiled vars:  !0 = $total, !1 = $argv, !2 = $fib
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   86     0  E >   ISSET_ISEMPTY_DIM_OBJ                         0          !1, 1
          1      > JMPZ                                                     ~3, ->5
          2    >   FETCH_DIM_R                                      ~4      !1, 1
          3        QM_ASSIGN                                        ~5      ~4
          4      > JMP                                                      ->6
          5    >   QM_ASSIGN                                        ~5      25
          6    >   ASSIGN                                                   !0, ~5
   88     7        NEW                                              $7      'Fibonacci'
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !2, $7
   89    10        INIT_FCALL                                               'test'
         11        SEND_VAR                                                 !0
         12        INIT_ARRAY                                       ~10     !2
         13        ADD_ARRAY_ELEMENT                                ~10     'iterative'
         14        SEND_VAL                                                 ~10
         15        DO_FCALL                                      0          
   90    16        INIT_FCALL                                               'test'
         17        SEND_VAR                                                 !0
         18        INIT_ARRAY                                       ~12     !2
         19        ADD_ARRAY_ELEMENT                                ~12     'binet'
         20        SEND_VAL                                                 ~12
         21        DO_FCALL                                      0          
   91    22        INIT_FCALL                                               'test'
         23        SEND_VAR                                                 !0
         24        INIT_ARRAY                                       ~14     !2
         25        ADD_ARRAY_ELEMENT                                ~14     'memoized'
         26        SEND_VAL                                                 ~14
         27        DO_FCALL                                      0          
   94    28        IS_SMALLER_OR_EQUAL                                      !0, 30
         29      > JMPZ                                                     ~16, ->36
   95    30    >   INIT_FCALL                                               'test'
         31        SEND_VAR                                                 !0
         32        INIT_ARRAY                                       ~17     !2
         33        ADD_ARRAY_ELEMENT                                ~17     'recursive'
         34        SEND_VAL                                                 ~17
         35        DO_FCALL                                      0          
   96    36    > > RETURN                                                   1

Function test:
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 = 11
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 11
Branch analysis from position: 17
Branch analysis from position: 11
filename:       /in/5ksoN
function name:  test
number of ops:  30
compiled vars:  !0 = $total, !1 = $callback, !2 = $t, !3 = $x, !4 = $finish
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   76     2        FETCH_DIM_R                                      ~5      !1, 1
          3        CONCAT                                           ~6      ~5, '%3A%0A'
          4        ECHO                                                     ~6
   77     5        INIT_FCALL                                               'microtime'
          6        SEND_VAL                                                 <true>
          7        DO_ICALL                                         $7      
          8        ASSIGN                                                   !2, $7
   78     9        ASSIGN                                                   !3, 0
         10      > JMP                                                      ->15
   79    11    >   INIT_USER_CALL                                1          'call_user_func', !1
         12        SEND_USER                                                !3
         13        DO_FCALL                                      0          
   78    14        PRE_INC                                                  !3
         15    >   IS_SMALLER                                               !3, !0
         16      > JMPNZ                                                    ~12, ->11
   81    17    >   INIT_FCALL                                               'microtime'
         18        SEND_VAL                                                 <true>
         19        DO_ICALL                                         $13     
         20        SUB                                              ~14     $13, !2
         21        ASSIGN                                                   !4, ~14
   82    22        MUL                                              ~16     !4, 1000
         23        CONCAT                                           ~17     ~16, '+ms+%28'
         24        DIV                                              ~18     !4, !0
         25        MUL                                              ~19     ~18, 1000
         26        CONCAT                                           ~20     ~17, ~19
         27        CONCAT                                           ~21     ~20, '%29%0A%0A'
         28        ECHO                                                     ~21
   83    29      > RETURN                                                   null

End of function test

Class Fibonacci:
Function recursive:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5ksoN
function name:  recursive
number of ops:  18
compiled vars:  !0 = $n
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
   19     1        IS_EQUAL                                                 !0, 0
          2      > JMPZ                                                     ~1, ->4
   20     3    > > RETURN                                                   0
   23     4    >   IS_EQUAL                                                 !0, 1
          5      > JMPZ                                                     ~2, ->7
   24     6    > > RETURN                                                   1
   27     7    >   INIT_METHOD_CALL                                         'recursive'
          8        SUB                                              ~3      !0, 1
          9        SEND_VAL_EX                                              ~3
         10        DO_FCALL                                      0  $4      
         11        INIT_METHOD_CALL                                         'recursive'
         12        SUB                                              ~5      !0, 2
         13        SEND_VAL_EX                                              ~5
         14        DO_FCALL                                      0  $6      
         15        ADD                                              ~7      $4, $6
         16      > RETURN                                                   ~7
   28    17*     > RETURN                                                   null

End of function recursive

Function iterative:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 5
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 5
Branch analysis from position: 11
Branch analysis from position: 5
filename:       /in/5ksoN
function name:  iterative
number of ops:  13
compiled vars:  !0 = $n, !1 = $a, !2 = $b, !3 = $i, !4 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
   35     1        ASSIGN                                                   !1, 0
   36     2        ASSIGN                                                   !2, 1
   38     3        ASSIGN                                                   !3, 0
          4      > JMP                                                      ->9
   39     5    >   ASSIGN                                                   !4, !1
   40     6        ASSIGN                                                   !1, !2
   41     7        ASSIGN_OP                                     1          !2, !4
   38     8        PRE_INC                                                  !3
          9    >   IS_SMALLER                                               !3, !0
         10      > JMPNZ                                                    ~12, ->5
   44    11    > > RETURN                                                   !1
   45    12*     > RETURN                                                   null

End of function iterative

Function binet:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5ksoN
function name:  binet
number of ops:  23
compiled vars:  !0 = $n, !1 = $phi
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   RECV                                             !0      
   53     1        INIT_FCALL                                               'sqrt'
          2        SEND_VAL                                                 5
          3        DO_ICALL                                         $2      
          4        ADD                                              ~3      1, $2
          5        DIV                                              ~4      ~3, 2
          6        ASSIGN                                                   !1, ~4
   55     7        INIT_FCALL                                               'pow'
          8        SEND_VAR                                                 !1
          9        SEND_VAR                                                 !0
         10        DO_ICALL                                         $6      
         11        INIT_FCALL                                               'pow'
         12        SUB                                              ~7      1, !1
         13        SEND_VAL                                                 ~7
         14        SEND_VAR                                                 !0
         15        DO_ICALL                                         $8      
         16        SUB                                              ~9      $6, $8
         17        INIT_FCALL                                               'sqrt'
         18        SEND_VAL                                                 5
         19        DO_ICALL                                         $10     
         20        DIV                                              ~11     ~9, $10
         21      > RETURN                                                   ~11
   56    22*     > RETURN                                                   null

End of function binet

Function memoized:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 17
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/5ksoN
function name:  memoized
number of ops:  21
compiled vars:  !0 = $n
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
   63     1        FETCH_OBJ_IS                                     ~1      'cache'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      ~1, !0
          3        BOOL_NOT                                         ~3      ~2
          4      > JMPZ                                                     ~3, ->17
   64     5    >   INIT_METHOD_CALL                                         'memoized'
          6        SUB                                              ~6      !0, 1
          7        SEND_VAL_EX                                              ~6
          8        DO_FCALL                                      0  $7      
          9        INIT_METHOD_CALL                                         'memoized'
         10        SUB                                              ~8      !0, 2
         11        SEND_VAL_EX                                              ~8
         12        DO_FCALL                                      0  $9      
         13        ADD                                              ~10     $7, $9
         14        FETCH_OBJ_W                                      $4      'cache'
         15        ASSIGN_DIM                                               $4, !0
         16        OP_DATA                                                  ~10
   67    17    >   FETCH_OBJ_R                                      ~11     'cache'
         18        FETCH_DIM_R                                      ~12     ~11, !0
         19      > RETURN                                                   ~12
   68    20*     > RETURN                                                   null

End of function memoized

End of class Fibonacci.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.93 ms | 1411 KiB | 23 Q