Tumble Confused Device

.oOo.

What about Fortran for speed

As I was playing with fibonacci sequences to try different languages I decided to return to one of my first/oldest/smelliest languages: fortran.

I coded a recursive function like this:

program fibonacci
   print *, "Fibonacci 44:", fibo(44)
contains
   recursive integer function fibo(n) result(a)
      implicit none
      integer, intent(in) :: n
      if (n < 2) then
         a = n
      else
         a = fibo(n - 2) + fibo(n - 1)
      end if
   end function fibo
end program fibonacci

You can compare this to the C Version and think that these two will be similar.

Until they are compiled!!

With no optimization, their running times are comparable, but when using -O2 the fortran version smokes the C version. What? I expected them to be similar, but we are always surprised.

Other versions of fibo are on github.

.oOo.