如何更快地制作这个简单的fortran 90代码?

Gud*_*ddu 5 c++ performance gfortran fortran90

我试图比较一个简单代码的计算时间,使用Fortran 90和C++来计算整数的立方总和,因为我听说它们在类似的级别上很快.我使用gfortran和g ++(在Mac OSX上)来编译这些代码.

有人可以指出为什么Fortran 90代码比同等的C++代码(12秒)花费的时间(49秒)要多得多吗?我只知道C++是行专业,而Fortran是专栏专栏,但我认为这与这些代码无关.如何让这个fortran90代码更快?任何提示将不胜感激.谢谢.

Fortran代码和编译 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
Run Code Online (Sandbox Code Playgroud)

输出是

 final sum    1.63840000204800000399876515667619840E+0036
 the time taken is    48.6228256
Run Code Online (Sandbox Code Playgroud)

C++代码和编译 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;
}
Run Code Online (Sandbox Code Playgroud)

输出是

final sum 1.6384e+36
the time taken is 12.0104
Run Code Online (Sandbox Code Playgroud)

Mar*_*ayr 6

我不是Fortran专家,但似乎是这样

real (kind=16) :: ssum
Run Code Online (Sandbox Code Playgroud)

声明一个四倍精度(16字节)浮点数,可能在您的硬件上用软件模拟.您的C++代码使用的long double对应于扩展精度(10字节)浮点数,这可以由您的硬件完成(并且速度更快).请注意,long double在所有平台上都不是10字节的浮点数,例如,它可能与double某些平台上的浮点数相同.我认为Windows和MSVC都是如此.要在fortran中获得扩展的精度浮点数,请使用:

real (kind=10) :: ssum
Run Code Online (Sandbox Code Playgroud)