TL DR: As Chuck already mentioned, Ruby does not have TCO. However, doing one recursion, not two, has a big impact on how many stacks you use and how many iterations are done. With this answer, I just want to point out that at some point, the memoization version is better than the iterative version. NB: I'm not a ruby ββprogrammer. This may not be an idiomatic code.
The test shows that the iterative approach is so fast that it can generate Fibo 1. 1.50 from scratch as fast as your memoization version reuses calculations in every method call above 3.
I think that 1..50 is done so quickly that it is not very reliable to see if the iteration is actually faster. I changed the memopization version to:
Then I changed the loop to this:
(1..5000).each { |number| fibo(number)
Here are the results on my computer:
Iteration: 6.260000 0.010000 6.270000 ( 6.273362) Memoization: 0.000000 0.000000 0.000000 ( 0.006943)
I used:
ruby -v ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
Increasing the memoization version to 1.50,000 still returned much faster than the iteration version. The reason is that iteration starts from scratch every time, while the memoization version has a more inefficient algorithm, but memoization only makes it max. 2 times for each number, since we have fib(n-1) and fib(n-2) in the array when calculating fib (n) `.
The slowest one has O(fib(n)) , of course. Iterative has O(n) . Since memoization, fib(n-2) free to calculate fib(n-1) , so we go back to O(n) , but in your test, you calculate the previous fibonacci number before the next, so in practice each individual iteration from 1..x equal to O(1) . If you start with the highest number, the first iteration will be O(n) , and each next will be O(1) .