Performance of Fibonacci numbers algorithms

Performance of algorithms is important topic if you want to write programs that work fast and doesn’t eat too much resources. In this example I will show you two implementations of famous Fibonacci numbers algorithm and let you compare how these two implementations perform. This posting will be also introduction to my next posting to keep it smaller and to keep focus on point.

You can read more about Fibonacci numbers from Data Structures and Algorithms with Object-Oriented Patterns in C#, chapter Example-Fibonacci Numbers.

Recursive implementation

Our first implementation of Fibonacci numbers uses recursive algorithm. Recursive algorithm is very short and also easy to read.


public int FibonacciRecursive(int x)
{
   
if
(x == 0 || x == 1)
       
return
x;

   
return FibonacciRecursive(x - 1) +
            FibonacciRecursive(x - 2);
}

Experienced programmers should see here two dangers. First of them gets clear to others at the end of this posting. Second of them is simple: recursive code is harder to debug than flat code.

Flat implementation

The other implementation is not so short but it doesn’t call our algorithm again and again. It is also not so easy to read as first implementation.


public int Fibonacci(int x)
{
   
var
previousValue = -1;
   
var
currentResult = 1;

   
for (var
i = 0; i <= x; ++i)
    {
       
var
sum = currentResult + previousValue;
        previousValue = currentResult;
        currentResult = sum;
    }

   
return currentResult;
}

Before I say anything else let’s compare performance of those implementations.

Performance

Now let’s see how these implementations perform. I am sure that less experienced programmers will check if these implementations give same results and they will try them with pretty small inputs. But… look at the results.

Fibonacci: performance comparison between recursive and flat methods in C#

Horizontal axis shows input integers. Vertical axis shows us time in seconds that calculation took. For smaller inputs both implementations perform very well. Near 30 performance of recursive implementations starts going worse. For the time of last input (that is 41) we are sure that recursive implementation is a highway to hell. But performance of flat implementation is still okay.

This example is good illustration why we must test all the implementations we have before making decision which implementation to use.

Update: Bart Czernicki asked in his comment about F# performance when using recursive calls. I tried it out and F# performs very well in this scenario. In F# you can use recursive calls without any problems as my tests showed me.


One thought on “Performance of Fibonacci numbers algorithms

Leave a Reply

Your email address will not be published. Required fields are marked *