-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory-allocations.cpp
More file actions
75 lines (68 loc) · 2.28 KB
/
memory-allocations.cpp
File metadata and controls
75 lines (68 loc) · 2.28 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
71
72
73
74
75
#include <benchmark/benchmark.h>
#include <memory>
class DataElement {
public:
DataElement(int key, int filler1, int filler2, int filler3, int filler4, int filler5, int filler6, int filler7)
: key(key), filler1(filler1), filler2(filler2), filler3(filler3), filler4(filler4), filler5(filler5),
filler6(filler6), filler7(filler7) {};
int key;
int filler1;
int filler2;
int filler3;
int filler4;
int filler5;
int filler6;
int filler7;
};
static constexpr auto NumElements = 2048;
static void HeapAllocation(benchmark::State &state) {
for (auto _ : state) {
std::vector<std::unique_ptr<DataElement>> array;
array.reserve(NumElements);
for (int i = 0; i < NumElements; i++) {
array.emplace_back(std::make_unique<DataElement>(1, 1, 2, 3, 4, 5, 6, 7));
}
benchmark::DoNotOptimize(array);
}
}
BENCHMARK(HeapAllocation);
static void StackAllocation(benchmark::State &state) {
for (auto _ : state) {
std::vector<DataElement> array;
array.reserve(NumElements);
for (int i = 0; i < NumElements; i++) {
array.emplace_back(1, 1, 2, 3, 4, 5, 6, 7);
}
benchmark::DoNotOptimize(array);
}
}
BENCHMARK(StackAllocation);
static void DataLocal(benchmark::State &state) {
std::vector<DataElement> array;
array.reserve(NumElements);
for (int i = 0; i < NumElements; i++) {
array.emplace_back(std::rand(), 1, 2, 3, 4, 5, 6, 7);
}
for (auto _ : state) {
auto maxElement = std::max_element(array.begin(), array.end(), [](const auto &a, const auto &b) {
return a.key < b.key;
});
benchmark::DoNotOptimize(maxElement);
}
}
BENCHMARK(DataLocal);
static void DataIndirection(benchmark::State &state) {
std::vector<std::unique_ptr<DataElement>> array;
array.reserve(NumElements);
for (int i = 0; i < NumElements; i++) {
array.emplace_back(std::make_unique<DataElement>(std::rand(), 1, 2, 3, 4, 5, 6, 7));
}
for (auto _ : state) {
auto maxElement = std::max_element(array.begin(), array.end(), [](const auto &a, const auto &b) {
return a->key < b->key;
});
benchmark::DoNotOptimize(maxElement);
}
}
BENCHMARK(DataIndirection);
BENCHMARK_MAIN();