Skip to content

Instantly share code, notes, and snippets.

@hazelnusse
Forked from certik/README.md
Created August 28, 2012 07:10
Show Gist options
  • Save hazelnusse/3495758 to your computer and use it in GitHub Desktop.
Save hazelnusse/3495758 to your computer and use it in GitHub Desktop.
C vs Fortran benchmark

On my machine, the timings are:

Fortran:

$ time ./a_f
   249999999500000000

real	0m1.142s
user	0m1.138s
sys	0m0.002s

C:

$ time ./a_c
249999999500000000

real	0m0.778s
user	0m0.776s
sys	0m0.001s

C++:

time ./a_c++ 
249999999500000000

real	0m0.751s
user	0m0.750s
sys	0m0.001s

So C++ is 1.142 / 0.751 = 1.521 times faster than Fortran.

// 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);
}
// Compile with:
// g++ -O3 -march=native -ffast-math -funroll-loops a.cc
#include <iostream>
int main()
{
int i = 0;
long int s = 0;
for (int i = 0; i < 1000000000; ++i)
if (i % 2 == 0) s += i;
std::cout << s << std::endl;
}
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
all : a_f a_c a_c++
a_f : a.f90
gfortran -O3 -march=native -ffast-math -funroll-loops a.f90 -o a_f
a_c : a.c
gcc -O3 -march=native -ffast-math -funroll-loops a.c -o a_c
a_c++ : a.cc
g++ -O3 -march=native -ffast-math -funroll-loops a.cc -o a_c++
clean :
rm -rf a_f a_c a_c++
.PHONY : all clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment