-
Notifications
You must be signed in to change notification settings - Fork 0
/
array2Dcache.cpp
71 lines (61 loc) · 1.85 KB
/
array2Dcache.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Purpose: Multidimensional arrays and cache
* Author: Emanuele Rizzolo
* Class: 3XIN
* Date: 2020/01/07
* Note:
*/
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
#define DEBUG 1
#define DIM 20000
/// global variable (visible everywhere)
int v[DIM][DIM];
/// main function
int main(int argc, char *argv[])
{
// random asssignment
for (int i = 0; i < DIM; i++)
{
for (int j = 0; j < DIM; j++)
{
v[i][j] = rand();
}
}
clock_t startTime = clock(); // read clock
// outer loop on first index, inner loop on second
// follows allocation strategy: optimal cache usage
// time 1s approximately on my platform
long long int sumByFirstIndex = 0;
for (int f = 0; f < DIM; f++)
{
for (int s = 0; s < DIM; s++)
{
sumByFirstIndex += v[f][s];
}
}
cout << "somma = " << sumByFirstIndex << endl;
clock_t endTime = clock(); // read clock again
double duration = 1000.0 * (endTime - startTime) / CLOCKS_PER_SEC;
cout << " completed in " << duration << "ms" << endl;
clock_t startTime2 = clock(); // read clock
long long int sumBySecondIndex = 0;
// outer loop on second index, inner loop on first
// does not follow allocation strategy: non optimal cache usage
// time 12s approximately on my platform
for (int s = 0; s < DIM; s++)
{
for (int f = 0; f < DIM; f++)
{
sumBySecondIndex += v[f][s];
}
}
cout << "somma = " << sumBySecondIndex << endl;
clock_t endTime2 = clock(); // read clock again
double duration2 = 1000.0 * (endTime2 - startTime2) / CLOCKS_PER_SEC;
cout << " completed in " << duration2 << "ms" << endl;
/// successful termination
return 0;
}