Last active
December 12, 2015 05:18
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To compile:
Compare for 100 elements:
Compare for million(1E6) elements:
Compare for billion(1E9) elements: