-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBenchGuardedVectorExample.cpp
More file actions
175 lines (165 loc) · 4.5 KB
/
BenchGuardedVectorExample.cpp
File metadata and controls
175 lines (165 loc) · 4.5 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "../examples/GuardedVectorExample.h"
#include <nanobench.h>
#include <chrono>
#include <vector>
#if defined(__has_feature) // Clang
# if __has_feature(thread_sanitizer)
# define USING_THREAD_SANITIZER 1
# else
# define USING_THREAD_SANITIZER 0
# endif
#elif defined(__SANITIZE_THREAD__) // GCC
# define USING_THREAD_SANITIZER 1
#else
# define USING_THREAD_SANITIZER 0
#endif
using namespace std::chrono_literals;
const auto minEpoch = 100ms;
template<typename T>
int BenchVector(ankerl::nanobench::Bench& bench, bool withNoReserve)
{
#ifdef NDEBUG
const size_t nbPushBacksPerIteration[] = { 1'000, 100'000, 10'000'000 };
#else
const size_t nbPushBacksPerIteration[] = { 1'000 };
#endif
uint64_t x = 1;
{
std::vector<T> vector;
for (size_t size : nbPushBacksPerIteration)
{
vector.reserve(size); // Don't measure allocator
bench.complexityN(size)
.minEpochTime(minEpoch)
.run("std::vector.push_back", [&] {
for (int i = 0; i < size; i++)
{
vector.push_back({ x });
}
ankerl::nanobench::doNotOptimizeAway(x += vector.size());
vector.clear();
});
}
}
#if !USING_THREAD_SANITIZER
#ifndef NDEBUG // Only gives different perf in debug builds
{
ExampleGuardedVector<T> guardedvector;
for (size_t size : nbPushBacksPerIteration)
{
guardedvector.reserve(size); // Don't measure allocator
bench.complexityN(size)
.minEpochTime(minEpoch)
.run("guardedvector.push_back_noguard", [&] {
for (int i = 0; i < size; i++)
{
// This is benched to demonstrate that the guard is not much more expensive thant a simple forwarding call in debug...
guardedvector.push_back_noguard({ x });
}
ankerl::nanobench::doNotOptimizeAway(x += guardedvector.size());
guardedvector.clear();
});
}
}
#endif
{
ExampleGuardedVector<T> guardedvector;
for (size_t size : nbPushBacksPerIteration)
{
guardedvector.reserve(size); // Don't measure allocator
bench.complexityN(size)
.minEpochTime(minEpoch)
.run("guardedvector.push_back", [&] {
for (int i = 0; i < size; i++)
{
guardedvector.push_back({ x });
}
ankerl::nanobench::doNotOptimizeAway(x += guardedvector.size());
guardedvector.clear();
});
}
}
#endif //!USING_THREAD_SANITIZER
if (withNoReserve)
{
{
std::vector<T> vector;
for (size_t size : nbPushBacksPerIteration)
{
bench.complexityN(size)
.minEpochTime(minEpoch)
.run("std::vector.push_back - noreserve", [&] {
for (int i = 0; i < size; i++)
{
vector.push_back({ x });
}
ankerl::nanobench::doNotOptimizeAway(x += vector.size());
vector.clear();
vector.shrink_to_fit();
});
}
}
#if !USING_THREAD_SANITIZER
#ifndef NDEBUG // Only gives different perf in debug builds
{
ExampleGuardedVector<T> guardedvector;
for (size_t size : nbPushBacksPerIteration)
{
guardedvector.reserve(size); // Don't measure allocator
bench.complexityN(size)
.minEpochTime(minEpoch)
.run("guardedvector.push_back_noguard - noreserve", [&] {
for (int i = 0; i < size; i++)
{
// This is benched to demonstrate that the guard is not much more expensive thant a simple forwarding call in debug...
guardedvector.push_back_noguard({ x });
}
ankerl::nanobench::doNotOptimizeAway(x += guardedvector.size());
guardedvector.clear();
guardedvector.shrink_to_fit();
});
}
}
#endif
{
ExampleGuardedVector<T> guardedvector;
for (size_t size : nbPushBacksPerIteration)
{
bench.complexityN(size)
.minEpochTime(minEpoch)
.run("guardedvector.push_back - noreserve", [&] {
for (int i = 0; i < size; i++)
{
guardedvector.push_back({ x });
}
ankerl::nanobench::doNotOptimizeAway(x += guardedvector.size());
guardedvector.clear();
guardedvector.shrink_to_fit();
});
}
}
#endif //!USING_THREAD_SANITIZER
}
return x;
}
int main() {
int x = 0;
x += BenchVector<uint64_t>(ankerl::nanobench::Bench().title("Vector of uint64_t"), true);
struct Payload16B {
uint64_t storage[2];
};
x += BenchVector<Payload16B>(ankerl::nanobench::Bench().title("Vector of uint64_t*2"), false);
struct Payload32B {
uint64_t storage[4];
};
x += BenchVector<Payload32B>(ankerl::nanobench::Bench().title("Vector of uint64_t*4"), false);
struct PayloadString {
PayloadString(uint64_t v) {
(void)v;
storage += char(v);
}
std::string storage;
};
x += BenchVector<PayloadString>(ankerl::nanobench::Bench().title("Vector of std::string"), false);
return x;
}