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 |
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