-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_constexpr_map.cpp
218 lines (184 loc) · 6.36 KB
/
bench_constexpr_map.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <benchmark/benchmark.h>
#include <iostream>
#include <array>
#include <string_view>
#include <algorithm>
#include <cstring>
#include <functional>
#include <random>
#include <string>
#include <map>
using namespace std::literals::string_view_literals;
auto generate_random_alphanumeric_string(std::size_t len) -> std::string {
static constexpr auto chars =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
std::random_device rd; //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
auto dist = std::uniform_int_distribution{{}, std::strlen(chars) - 1};
auto result = std::string(len, '\0');
std::generate_n(begin(result), len, [&]() { return chars[dist(gen)]; });
return result;
}
auto makeVector(std::size_t len) -> std::vector<std::string> {
std::vector<std::string> vectorOfStrings(len);
auto genString = []() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(1, 20);
return generate_random_alphanumeric_string(dist(gen));
};
std::generate(vectorOfStrings.begin(), vectorOfStrings.end(), genString);
return vectorOfStrings;
}
static void NoCache(benchmark::State& state) {
std::vector<std::string> vectorOfStrings = makeVector(100);
vectorOfStrings[0] = "a";
vectorOfStrings[1] = "b";
for (auto _ : state) {
constexpr size_t manualFixSize = 26;
static constexpr std::array<std::pair<std::string_view, std::string_view>, manualFixSize > manualFixPairs {
{
{"a"sv, "A"sv},
{"b"sv, "B"sv},
{"c"sv, "C"sv},
{"d"sv, "C"sv},
{"e"sv, "C"sv},
{"f"sv, "C"sv},
{"g"sv, "C"sv},
{"h"sv, "C"sv},
{"i"sv, "C"sv},
{"j"sv, "C"sv},
{"k"sv, "C"sv},
{"l"sv, "C"sv},
{"m"sv, "C"sv},
{"n"sv, "C"sv},
{"o"sv, "C"sv},
{"p"sv, "C"sv},
{"q"sv, "C"sv},
{"r"sv, "C"sv},
{"s"sv, "C"sv},
{"t"sv, "C"sv},
{"u"sv, "C"sv},
{"v"sv, "C"sv},
{"w"sv, "C"sv},
{"x"sv, "C"sv},
{"y"sv, "C"sv},
{"z"sv, "C"sv},
}
};
for (const auto& val: vectorOfStrings) {
auto it = std::find_if(manualFixPairs.cbegin(), manualFixPairs.cend(), [&val](const auto& p) { return p.first == val; });
if (it != manualFixPairs.cend()) {
auto myval = it->second;
benchmark::DoNotOptimize(myval);
}
}
}
}
// Register the function as a benchmark
BENCHMARK(NoCache);
static void WithCache(benchmark::State& state) {
std::vector<std::string> vectorOfStrings = makeVector(100);
vectorOfStrings[0] = "a";
vectorOfStrings[1] = "b";
for (auto _ : state) {
constexpr size_t manualFixSize = 26;
static constexpr std::array<std::pair<std::string_view, std::string_view>, manualFixSize > manualFixPairs {
{
{"a"sv, "A"sv},
{"b"sv, "B"sv},
{"c"sv, "C"sv},
{"d"sv, "C"sv},
{"e"sv, "C"sv},
{"f"sv, "C"sv},
{"g"sv, "C"sv},
{"h"sv, "C"sv},
{"i"sv, "C"sv},
{"j"sv, "C"sv},
{"k"sv, "C"sv},
{"l"sv, "C"sv},
{"m"sv, "C"sv},
{"n"sv, "C"sv},
{"o"sv, "C"sv},
{"p"sv, "C"sv},
{"q"sv, "C"sv},
{"r"sv, "C"sv},
{"s"sv, "C"sv},
{"t"sv, "C"sv},
{"u"sv, "C"sv},
{"v"sv, "C"sv},
{"w"sv, "C"sv},
{"x"sv, "C"sv},
{"y"sv, "C"sv},
{"z"sv, "C"sv},
}
};
std::array<bool, manualFixSize> alreadyDones;
alreadyDones.fill(false);
for (const auto& val: vectorOfStrings) {
auto it = std::find_if(manualFixPairs.cbegin(), manualFixPairs.cend(),
[&val, &alreadyDones, idx = 0](const auto& p) mutable {
int i = idx++;
if (!alreadyDones[i] && (p.first == val)) {
alreadyDones[i] = true;
return true;
}
return false;
});
if (it != manualFixPairs.cend()) {
auto myval = it->second;
benchmark::DoNotOptimize(myval);
}
}
}
}
// Register the function as a benchmark
BENCHMARK(WithCache);
static void NotConstexpr(benchmark::State& state) {
std::vector<std::string> vectorOfStrings = makeVector(100);
vectorOfStrings[0] = "a";
vectorOfStrings[1] = "b";
for (auto _ : state) {
std::map<std::string_view, std::string_view> manualFixPairs
{
{"a"sv, "A"sv},
{"b"sv, "B"sv},
{"c"sv, "C"sv},
{"d"sv, "C"sv},
{"e"sv, "C"sv},
{"f"sv, "C"sv},
{"g"sv, "C"sv},
{"h"sv, "C"sv},
{"i"sv, "C"sv},
{"j"sv, "C"sv},
{"k"sv, "C"sv},
{"l"sv, "C"sv},
{"m"sv, "C"sv},
{"n"sv, "C"sv},
{"o"sv, "C"sv},
{"p"sv, "C"sv},
{"q"sv, "C"sv},
{"r"sv, "C"sv},
{"s"sv, "C"sv},
{"t"sv, "C"sv},
{"u"sv, "C"sv},
{"v"sv, "C"sv},
{"w"sv, "C"sv},
{"x"sv, "C"sv},
{"y"sv, "C"sv},
{"z"sv, "C"sv},
};
for (const auto& val: vectorOfStrings) {
auto it = manualFixPairs.find(val);
if (it != manualFixPairs.end()) {
auto myval = it->second;
manualFixPairs.erase(it);
benchmark::DoNotOptimize(myval);
}
}
}
}
// Register the function as a benchmark
BENCHMARK(NotConstexpr);