Skip to content

Instantly share code, notes, and snippets.

@Rhomboid
Created November 29, 2013 05:01
Show Gist options
  • Select an option

  • Save Rhomboid/7701768 to your computer and use it in GitHub Desktop.

Select an option

Save Rhomboid/7701768 to your computer and use it in GitHub Desktop.
Simplistic cache size measurement (C++11)
#include <iostream>
#include <sstream>
#include <vector>
#include <chrono>
#include <random>
#include <algorithm>
using namespace std;
using namespace std::chrono;
int main()
{
mt19937 rnd { 0xfeedbeefU };
const size_t repcnt = 128 * 1024 * 1024, trialcnt = 5;
stringstream holder;
typedef uint32_t test_type;
uniform_int_distribution<test_type> dist;
auto rndsource = [&]() { return dist(rnd); };
vector<test_type> data;
vector<double> results;
for(size_t sz = 256; sz <= 32 * 1024 * 1024; sz = sz * 9 / 8) {
generate_n(back_inserter(data), sz - data.size(), rndsource);
uniform_int_distribution<size_t> index { 0, sz };
results.clear();
for(size_t trial = 0; trial < trialcnt; trial++) {
test_type sum = 0;
auto start = high_resolution_clock::now();
for(size_t n = 0; n < repcnt; n++)
sum += data[index(rnd)];
auto end = high_resolution_clock::now();
results.push_back(duration_cast<nanoseconds>(end - start).count() / (double)repcnt);
holder << sum;
}
const auto best = *min_element(begin(results), end(results));
const auto byte_size = sz * sizeof(test_type);
cout << byte_size << ' ' << best << endl;
}
}
1024 6.50883
1152 6.50884
1296 6.50884
1456 6.50883
1636 6.50883
1840 6.50883
2068 6.50884
2324 6.50883
2612 6.50883
2936 6.50883
3300 6.50884
3712 6.50883
4176 6.50883
4696 6.50883
5280 6.50883
5940 6.50883
6680 6.50884
7512 6.50883
8448 6.50883
9504 6.50883
10692 6.50884
12028 6.50884
13528 6.50883
15216 6.50883
17116 6.50884
19252 6.50883
21656 6.50884
24360 6.50883
27404 6.50884
30828 6.50883
34680 6.50884
39012 6.62506
43888 6.62506
49372 6.62506
55540 6.62506
62480 6.62506
70288 6.62506
79072 6.62506
88956 6.62506
100072 6.62507
112580 6.62507
126652 6.62507
142480 6.62506
160288 6.62507
180324 6.62507
202864 6.62506
228220 6.62506
256744 6.74129
288836 6.74129
324940 6.85752
365556 6.97375
411248 6.97376
462652 7.08999
520480 7.20621
585540 7.20621
658732 7.20621
741072 7.20621
833704 7.32244
937916 7.32245
1055152 7.32245
1187044 7.32245
1335424 7.32245
1502352 7.32245
1690144 7.43867
1901412 7.32245
2139088 7.43867
2406472 7.5549
2707280 7.67113
3045688 7.78736
3426396 7.90358
3854692 7.90359
4336528 8.01981
4878592 8.25228
5488416 8.60097
6174468 9.06589
6946276 9.76326
7814560 11.0418
8791380 12.9014
9890300 14.6449
11126584 16.1559
12517404 17.3182
14082076 18.248
15842332 18.8291
17822620 19.4103
20050444 19.8752
22556748 20.2239
25376340 20.4564
28548380 20.9213
32116924 21.0375
36131536 21.3862
40647976 21.6186
45728972 21.8511
51445092 21.9673
57875728 22.0836
65110192 22.1998
73248964 22.6647
82405084 23.5945
92705716 23.7108
104293928 24.1757
117330668 24.5244
131997000 24.4081
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment