Skip to content

Instantly share code, notes, and snippets.

@certik
Created July 20, 2011 22:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save certik/1096061 to your computer and use it in GitHub Desktop.
Save certik/1096061 to your computer and use it in GitHub Desktop.
C vs Fortran benchmark

On my machine, the timings are:

Fortran:

$ time ./a.out 
   249999999500000000

real	0m0.510s
user	0m0.510s
sys	0m0.000s

C:

$ time ./a.out 
249999999500000000

real	0m0.730s
user	0m0.720s
sys	0m0.010s

So Fortran is 0.730 / 0.510 = 1.43 times faster.

// Compile with:
// gcc -O3 -march=native -ffast-math -funroll-loops a.c
#include "stdio.h"
int main()
{
int i = 0;
long int s = 0;
while (i < 1000000000) {
if (i % 2 == 0) s += i;
i++;
}
printf("%ld\n", s);
}
program test
! Compile with:
! gfortran -O3 -march=native -ffast-math -funroll-loops a.f90
implicit none
integer, parameter :: dp=kind(0.d0)
integer :: i
integer(8) :: s
s = 0
do i = 0, 1000000000-1
if (mod(i, 2) == 0) s = s + i
end do
print *, s
end program
@hazelnusse
Copy link

Ondrej, I just tried this with gcc and gfortran 4.7.2 and get very different results. See the dev branch on my fork: https://gist.github.com/3495758

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment