c++ - A simple test case between clang++/g++/gfortran -
i ran across question on scicomp involves computing sum. there, can see c++ , similar fortran implementation. interestingly saw fortran version faster 32%.
i thought, not sure result , tried regenerate situation. here (very slightly) different codes ran:
c++
#include <iostream> #include <complex> #include <cmath> #include <iomanip> int main () { const double alpha = 1; std::cout.precision(16); std::complex<double> sum = 0; const std::complex<double> = std::complex<double>(1,1)/std::sqrt(2.); (unsigned int k=1; k<10000000; ++k) { sum += std::pow(a, k)*std::pow(k, -alpha); if (k % 1000000 == 0) std::cout << k << ' ' << sum << std::endl; } return 0; }
fortran
implicit none integer, parameter :: dp = kind(0.d0) complex(dp), parameter :: i_ = (0, 1) real(dp) :: alpha = 1 complex(dp) :: s = 0 integer :: k k = 1, 10000000 s = s + ((i_+1)/sqrt(2._dp))**k * k**(-alpha) if (modulo(k, 1000000) == 0) print *, k, s end end
i compile above codes using gcc 4.6.3
, clang 3.0
on ubuntu 12.04 lts
machine -o3
flag. here's timings:
time ./a.out
gfortran
real 0m1.538s user 0m1.536s sys 0m0.000s
g++
real 0m2.225s user 0m2.228s sys 0m0.000s
clang
real 0m1.250s user 0m1.244s sys 0m0.004s
interestingly can see fortran
code faster c++
same 32% when gcc
used. using clang
, however, can see c++
code runs faster 19%. here questions:
- why g++ generated code slower gfortran? since same compiler family mean (this) fortran code can translated faster code? case fortran vs c++?
- why
clang
doing here? there fortran front-end llvm compiler? if there, code generated 1 faster?
update:
using -ffast-math -o3
options generates following results:
gfortran
real 0m1.515s user 0m1.512s sys 0m0.000s
g++
real 0m1.478s user 0m1.476s sys 0m0.000s
clang
real 0m1.253s user 0m1.252s sys 0m0.000s
npw g++
version running fast gfortran
, still clang
faster both. adding -fcx-fortran-rules
above options not change results
i believe problem in output part. well-known c++ streams (std::cout
) inefficient. while different compilers may optimize this, idea rewrite critical performance parts using c printf
function instead of std::cout
.
Comments
Post a Comment