-
Notifications
You must be signed in to change notification settings - Fork 0
/
test01-opt.cpp
79 lines (70 loc) · 1.47 KB
/
test01-opt.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
72
73
74
75
76
77
78
79
// Sequential access, optimized
// sizeof(TestStruct) == 64
#include <iostream>
#include <chrono>
const unsigned NUM_TESTS = 1000;
const unsigned PER_TEST = 10000;
struct TestStruct {
int a;
int b;
int c;
int d;
int e;
int f;
int g;
int h;
int i;
int j;
int k;
int l;
int m;
int n;
int o;
bool b0, b1, b2, b3;
};
void testFunc(TestStruct* array) {
auto start = std::chrono::steady_clock::now();
int sum = 0;
bool test = true;
// Test here
for (unsigned i = 0; i < PER_TEST; i++) {
// reads
sum += array[i].a;
sum += array[i].b;
sum += array[i].c;
sum += array[i].d;
test &= array[i].b0;
sum += array[i].e;
sum += array[i].f;
sum += array[i].g;
sum += array[i].h;
test &= array[i].b1;
sum += array[i].i;
sum += array[i].j;
sum += array[i].k;
sum += array[i].l;
test &= array[i].b2;
sum += array[i].m;
sum += array[i].n;
sum += array[i].o;
test &= array[i].b3;
// write so can't be optimized
array[i].o = sum;
array[i].b3 = test;
}
auto end = std::chrono::steady_clock::now();
#ifdef SHOW_OUTPUT
std::cout << (end - start).count() << std::endl;
#endif
}
int main() {
// Allocate test arrays here, so they don't get optimized out
TestStruct* testArray[NUM_TESTS];
for (unsigned i = 0; i < NUM_TESTS; i++) {
testArray[i] = new TestStruct[PER_TEST];
}
for (unsigned i = 0; i < NUM_TESTS; i++) {
testFunc(testArray[i]);
}
return 0;
}