Last active
November 11, 2016 22:05
-
-
Save arthurpham/04e9a893523fce8ff12f378176cc1dd0 to your computer and use it in GitHub Desktop.
Loop
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 <string> | |
#include <ctime> | |
#define N 1000 | |
double array_sum(double a[N][N]) | |
{ | |
int i,j; | |
double s; | |
s=0; | |
for (i=0;i<N;++i) | |
{ | |
for (j=0;j<N;++j) | |
{ | |
s += a[i][j]; | |
} | |
} | |
return s; | |
} | |
double array_sum2(double a[N][N]) | |
{ | |
int i,j; | |
double s; | |
s=0; | |
for (j=0;j<N;++j) | |
{ | |
for (i=0;i<N;++i) | |
{ | |
s += a[i][j]; | |
} | |
} | |
return s; | |
} | |
int main(int argc, char** argv) | |
{ | |
using namespace std; | |
double a[N][N]; | |
int i,j; | |
for (j=0;j<N;++j) | |
{ | |
for (i=0;i<N;++i) | |
{ | |
a[i][j] = 0.01; | |
} | |
} | |
{ | |
double sum = 0.0; | |
clock_t begin = clock(); | |
for (i=0;i<2000;++i) | |
{ | |
sum+=array_sum(a); | |
} | |
clock_t end = clock(); | |
std::cout << "sum = " << sum << std::endl; | |
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; | |
std::cout << "elapsed_secs " << elapsed_secs << std::endl; | |
} | |
{ | |
double sum = 0.0; | |
clock_t begin = clock(); | |
for (i=0;i<2000;++i) | |
{ | |
sum+=array_sum2(a); | |
} | |
clock_t end = clock(); | |
std::cout << "sum = " << sum << std::endl; | |
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; | |
std::cout << "elapsed_secs " << elapsed_secs << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment