Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make this simple fortran 90 code faster?

I am trying to compare computation times of a simple code to compute sum of cubes of integers using both Fortran 90 and C++ since I had heard they are fast on similar levels. I use gfortran and g++ (on Mac OSX) to compile these codes.

Can somebody kindly point out why the Fortran 90 code takes so much more time (49 seconds) than its equivalent C++ code (12 seconds)? Only thing I know that C++ is row major and Fortran is column major but I don't think that is relevant for these codes. How can I make this fortran90 code faster? Any tips will be appreciated. Thanks.

Fortran code and compiling with gfortran -o bb1 code15.f90

program code15 
implicit none

double precision, dimension(:), allocatable :: a
integer (kind=8) :: n,i
real (kind=16) :: ssum
real :: ts1, ts2

call cpu_time(ts1)
n = 1600000000
allocate(a(n))
ssum=0.0

do i=1,n
    a(i)=i
    ssum=ssum+a(i)*a(i)*a(i)
end do

print *, 'final sum ', ssum
deallocate(a) 
call cpu_time(ts2)
print *,'the time taken is ',ts2-ts1

end program

Output is

 final sum    1.63840000204800000399876515667619840E+0036
 the time taken is    48.6228256

C++ code and compiling with g++ -o bb1 code10.cpp

#include <iostream>
#include <time.h>
using namespace std;

main()
{
    long int n,i;
    long double ssum;

    clock_t starttime = clock();
    n=1600000000;
    double *a = new double[n];
    ssum=0;

    for(i=0; i<n; i++)
    {
        a[i]=i+1;
        ssum=ssum+a[i]*a[i]*a[i];
    }

    cout << "final sum " << ssum << endl;
    delete [ ]a;
    cout << "the time taken is "
         << (double)( clock() - starttime ) / (double)CLOCKS_PER_SEC
         << endl;
}

output is

final sum 1.6384e+36
the time taken is 12.0104
like image 914
Guddu Avatar asked Jun 30 '14 05:06

Guddu


People also ask

What makes Fortran so fast?

One of the reasons for this has to do with aliasing . Fortran has a syntax that is flexible enough for numerical computations but limited in some ways that allow better optimizing. This is the reason why fortran code can be faster than C or C++ code.

Which is faster C or Fortran?

Judging the performance of programming languages, usually C is called the leader, though Fortran is often faster. New programming languages commonly use C as their reference and they are really proud to be only so much slower than C.

Is Fortran faster than C ++?

The benchmarks where Fortran is much slower than C++ involve processes where most of the time is spent reading and writing data, for which Fortran is known to be slow. So, altogether, C++ is just as fast as Fortran and often a bit faster.


1 Answers

I am not a Fortran expert, but it seems that

real (kind=16) :: ssum

declares a quadruple precision (16 byte) floating point number, which is probably emulated in software on your hardware. Your C++ code uses a long double which corresponds to an extended precision (10 byte) floating point number, which can be done by your hardware (and is much faster). Please note that long double is not a 10-byte floating point number on all platforms, it may be the same thing as a double on some platforms, for example. I think this is true for Windows and MSVC. To get an extended precision floating point number in fortran, use:

real (kind=10) :: ssum
like image 118
Markus Mayr Avatar answered Sep 21 '22 15:09

Markus Mayr