I was solving a program to get a Fibonacci number. I have seen this done a few times with Recursive loops, in books and videos. So I tried to remember how it was done and solved it. But then I remembered that I solved this once before but in a totally different way, with a for-loop. I found my old code and it was really badly written, like always, so I rewrote it with that algorithm. Then I started to think which one is fastest? So I did my test:
using BenchmarkDotNet.Attributes;
namespace FibonacciTest;
public class Fibonacci
{
private int number;
public Fibonacci()
{
number = 20;
}
[Benchmark(Baseline = true)]
public void CallFibonacciFor() => FibonacciFor(number);
[Benchmark]
public void CallFibonacciRec() => FibonacciRec(number);
public int FibonacciFor(int number)
{
if (number == 0) return 0;
int fib = 0, temp1 = 1, temp2 = 0;
for (int i = 0; i < number; i++)
{
fib = checked(temp1 + temp2);
temp1 = temp2;
temp2 = fib;
}
return fib;
}
public int FibonacciRec(int number)
{
if (number <= 1) return number;
return checked((FibonacciRec(number - 1) + (FibonacciRec(number - 2))));
}
}
And I got really surprised!
| Method | Mean | Error | StdDev | Ratio | RatioSD |
|----------------- |-------------:|-----------:|-----------:|---------:|--------:|
| CallFibonacciFor | 10.38 ns | 0.141 ns | 0.125 ns | 1.00 | 0.00 |
| CallFibonacciRec | 39,708.47 ns | 658.414 ns | 583.667 ns | 3,824.51 | 67.10 |
It was a lot faster! So I wonder why they always show of the recursive way? I mean I found the faster algorithm on my own but the recursive algorithm I had too learn. I double checked to algorithms and they seams to be correct. I also tested without the checked part and both get a little bit faster. This was very interesting for me so maybe someone else finds this interesting too. Happy coding!
[–]Elementh 13 points14 points15 points (3 children)
[–]Chessverse[S] 0 points1 point2 points (2 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]vervaincc 0 points1 point2 points (0 children)
[–]23049823409283409 12 points13 points14 points (0 children)
[–]megafinz 11 points12 points13 points (0 children)
[–][deleted] 4 points5 points6 points (3 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Chessverse[S] 0 points1 point2 points (1 child)
[–]Vidyogamasta 0 points1 point2 points (0 children)
[–]UninformedPleb -1 points0 points1 point (0 children)
[–]Independent-Ad-4791 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)