There are various arguments that in some cases, Fortran can be faster than C, for example when it comes to aliasing and I often heard that it does better auto-vectorization than C (see here for some good discussion).
However, for simple functions as calculation the Fibonaci number and the Mandelbrot at some complex number with straight-forward solutions without any tricks and extra hints/keywords to the compiler, I would have expected that they really perform the same.
C implementation:
int fib(int n) { return n < 2 ? n : fib(n-1) + fib(n-2); } int mandel(double complex z) { int maxiter = 80; double complex c = z; for (int n=0; n<maxiter; ++n) { if (cabs(z) > 2.0) { return n; } z = z*z+c; } return maxiter; } Fortran implementation:
integer, parameter :: dp=kind(0.d0) ! double precision integer recursive function fib(n) result(r) integer, intent(in) :: n if (n < 2) then r = n else r = fib(n-1) + fib(n-2) end if end function integer function mandel(z0) result(r) complex(dp), intent(in) :: z0 complex(dp) :: c, z integer :: n, maxiter maxiter = 80 z = z0 c = z0 do n = 1, maxiter if (abs(z) > 2) then r = n-1 return end if z = z**2 + c end do r = maxiter end function Julia implementation:
fib(n) = n < 2 ? n : fib(n-1) + fib(n-2) function mandel(z) c = z maxiter = 80 for n = 1:maxiter if abs(z) > 2 return n-1 end z = z^2 + c end return maxiter end (The full code including other benchmark functions can be found here.)
According to the Julia homepage, Julia and Fortran (with -O3) perform better than C (with -O3) on these two functions.
How can that be?
dpdefined?