-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfor-loop.cc
More file actions
41 lines (33 loc) · 787 Bytes
/
for-loop.cc
File metadata and controls
41 lines (33 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
const int row = 1000;
const int col = 10000;
int buf[row][col];
int main(int argc, char * argv[])
{
srand(time(NULL));
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
buf[i][j] = rand();
}
}
clock_t st = clock();
int64_t sum = 0;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
sum += buf[i][j];
}
}
cout << "time for order 1: " << clock() - st << endl;
st = clock();
sum = 0;
for (int j = 0; j < col; ++j) {
for (int i = 0; i < row; ++i) {
sum += buf[i][j];
}
}
cout << "time for order 2: " << clock() - st << endl;
return 0;
}