Skip to content

Instantly share code, notes, and snippets.

@nagae
Last active December 12, 2015 05:18
Show Gist options
  • Select an option

  • Save nagae/4720750 to your computer and use it in GitHub Desktop.

Select an option

Save nagae/4720750 to your computer and use it in GitHub Desktop.
Compare C++ and Fortran in run time for calculating the sum of a large array.
#include <iostream>
#include <iomanip>
#include <sstream>
#include <time.h>
using namespace std;
int main(int argc, char *argv[] ) {
int i, max_itr;
double *val, sum, t1, t2, a, x0;
stringstream ss;
if (argc > 1) {
ss << argv[1];
ss >> max_itr;
} else {
max_itr = 100;
}
cout << "----------------------------------------" << endl;
cout << "C++" << endl;
cout << "----------" << endl;
cout << "num of itr:" << max_itr << endl;
t1 = clock();
val = new double[max_itr];
a = 3.5;
x0 = 0.2;
val[0] = a * x0 * (1-x0);
for (i=1; i<max_itr ;i++) {
val[i] = a * val[i-1] * (1-val[i-1]);
}
sum = 0.0;
for (i=0; i<max_itr; i++) {
sum = sum + val[i];
}
delete val;
t2 = clock();
cout << "sum:" << scientific << setprecision(16) << sum << endl;
cout << "Ellapsed time:" << fixed << setprecision(5) << (t2-t1)/CLOCKS_PER_SEC << endl;
cout << "----------------------------------------" << endl;
return 0;
}
program main
integer i, max_itr
integer,parameter :: buf_size = 100
double precision, allocatable :: val(:)
double precision sum, t1, t2, a, x0
character(buf_size) line_buf
if (iargc() >= 1) then
call getarg(1, line_buf)
read(line_buf, *) max_itr
else
max_itr = 100
end if
print '(a)', '----------------------------------------'
print '(a)', 'Fortran'
print '(a)', '----------'
print '("num of itr:"i0)', max_itr
call cpu_time(t1)
allocate( val(max_Itr) )
a = 3.5d0
x0 = 0.2d0
val(1) = a * x0 * (1-x0)
do i=2,max_itr
val(i) = a * val(i-1) * (1-val(i-1))
end do
sum = 0d0
do i=1,max_itr
sum = sum + val(i)
end do
deallocate(val)
call cpu_time(t2)
print '("sum:"e22.17)', sum
print '("Ellapsed time:"f10.5)', t2-t1
print '(a)', '----------------------------------------'
end program main
all:
g++ comp_time.cpp -o comp_time_c;
gfortran comp_time.f90 -o comp_time_f;
run:
./comp_time_c
./comp_time_f
run_E6:
./comp_time_c 1000000
./comp_time_f 1000000
run_E9:
./comp_time_c 1000000000
./comp_time_f 1000000000
clean:
rm -rf comp_time_c
rm -rf comp_time_f
@nagae
Copy link
Copy Markdown
Author

nagae commented Feb 6, 2013

To compile:

$ make -f comp_time.mak
++ comp_time.cpp -o comp_time_c;
gfortran comp_time.f90 -o comp_time_f;

Compare for 100 elements:

$ make -f comp_time.mak run
./comp_time_c
----------------------------------------
C++
----------
num of itr:100
sum:6.4667728017033156e+01
Ellapsed time:0.00000
----------------------------------------
./comp_time_f
----------------------------------------
Fortran
----------
num of itr:100
sum:.64667728017033156E+02
Ellapsed time:   0.00001
----------------------------------------

Compare for million(1E6) elements:

$ make -f comp_time.mak run_E6
./comp_time_c 1000000
----------------------------------------
C++
----------
num of itr:1000000
sum:6.4641049256157817e+05
Ellapsed time:0.01287
----------------------------------------
./comp_time_f 1000000
----------------------------------------
Fortran
----------
num of itr:1000000
sum:.64641049256157817E+06
Ellapsed time:   0.01925
----------------------------------------    

Compare for billion(1E9) elements:

$ make -f comp_time.mak run_E9
./comp_time_c 1000000000
----------------------------------------
C++
----------
num of itr:1000000000
sum:6.4641046613093603e+08
Ellapsed time:13.47483
----------------------------------------
./comp_time_f 1000000000
----------------------------------------
Fortran
----------
num of itr:1000000000
sum:.64641046613093603E+09
Ellapsed time:  19.36442
----------------------------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment