forked from betatim/NeedForSpeed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_sizes.cpp
More file actions
70 lines (58 loc) · 1.61 KB
/
cache_sizes.cpp
File metadata and controls
70 lines (58 loc) · 1.61 KB
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
#include <vector>
#include <iostream>
#include <chrono>
#include <thread>
// Measure passage of time by simply using the timers
// which are part of the standard library
class Timer {
private:
std::chrono::time_point<std::chrono::high_resolution_clock> start_time;
public:
void start() {
start_time = std::chrono::high_resolution_clock::now();
}
double stop() {
auto stop_time = std::chrono::high_resolution_clock::now();
return double(std::chrono::duration_cast<std::chrono::nanoseconds>(stop_time - start_time).count());
}
};
int main(void) {
int N = 2*52428800; // about 100MB
std::vector<char> data;
data.reserve(N);
// somewhere to read and write to fill
// the cahces with irrelevant stuff
std::vector<char> thrash;
thrash.reserve(N);
for (auto i=0; i<N; ++i) {
data.push_back(i);
}
for (auto i=0; i<N; ++i) {
thrash.push_back(i*3);
}
// these are only here to serve as a way of stopping
// the optimiser from getting rid of the loop bodies
// it is important that we return them at the end
auto blah(0), sum(0);
for (auto i=0; i<N; ++i) {
blah += thrash[i];
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000*2));
Timer timer;
for (auto step=1; step<3000; ++step) {
int reads(0);
timer.start();
for (auto s=0; s<N; s+=step) {
reads += 1;
sum += data[s];
}
auto t = timer.stop();
// do stuff with the rubbish array
for (auto i=0; i<N; ++i) {
blah += thrash[i];
}
t *= 1.e-9 * 1048576; // ns to seconds and bytes to MB
std::cout << step <<" "<< reads/t <<std::endl;
}
return sum+blah;
}