forked from vergecurrency/verge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicro_benchmark.cpp
More file actions
344 lines (277 loc) · 10.4 KB
/
micro_benchmark.cpp
File metadata and controls
344 lines (277 loc) · 10.4 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Standalone Micro-Benchmark for Modern C++ Migration Performance
// Compile with: clang++ -std=c++17 -O3 -DENABLE_CXX17 micro_benchmark.cpp -o micro_benchmark
// Or without C++17: clang++ -std=c++14 -O3 micro_benchmark.cpp -o micro_benchmark
#include <chrono>
#include <iostream>
#include <vector>
#include <string>
#include <memory>
#include <iomanip>
#if defined(ENABLE_CXX17) && __cplusplus >= 201703L
#include <string_view>
#include <optional>
#endif
using namespace std::chrono;
// Benchmark utilities
class BenchTimer {
public:
void start() { start_time = high_resolution_clock::now(); }
double stop() {
auto end_time = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(end_time - start_time);
return duration.count();
}
private:
high_resolution_clock::time_point start_time;
};
// Test data
std::vector<std::string> generateTestStrings(size_t count = 10000) {
std::vector<std::string> strings;
strings.reserve(count);
for (size_t i = 0; i < count; ++i) {
strings.push_back("This is test string number " + std::to_string(i) +
" used for performance benchmarking of modern C++ features");
}
return strings;
}
// Benchmark 1: String View vs Const Reference Performance
#if defined(ENABLE_CXX17) && __cplusplus >= 201703L
size_t processString_modern(std::string_view str) {
return str.length() + str.find("test");
}
#endif
size_t processString_legacy(const std::string& str) {
return str.length() + str.find("test");
}
void benchmarkStringProcessing() {
std::cout << "\n=== String Processing Benchmark ===\n";
auto test_strings = generateTestStrings(5000);
const int iterations = 100;
// Legacy approach
BenchTimer timer;
timer.start();
for (int iter = 0; iter < iterations; ++iter) {
size_t total = 0;
for (const auto& str : test_strings) {
total += processString_legacy(str);
}
// Prevent optimization
volatile size_t dummy = total;
(void)dummy;
}
double legacy_time = timer.stop();
#if defined(ENABLE_CXX17) && __cplusplus >= 201703L
// Modern approach with string_view
timer.start();
for (int iter = 0; iter < iterations; ++iter) {
size_t total = 0;
for (const auto& str : test_strings) {
total += processString_modern(str);
}
volatile size_t dummy = total;
(void)dummy;
}
double modern_time = timer.stop();
double improvement = ((legacy_time - modern_time) / legacy_time) * 100.0;
std::cout << "Legacy (const std::string&): " << std::fixed << std::setprecision(2)
<< legacy_time / 1e6 << " ms\n";
std::cout << "Modern (std::string_view): " << modern_time / 1e6 << " ms\n";
std::cout << "Improvement: " << improvement << "%\n";
#else
std::cout << "Legacy (const std::string&): " << std::fixed << std::setprecision(2)
<< legacy_time / 1e6 << " ms\n";
std::cout << "Modern (std::string_view): Not available (C++17 disabled)\n";
#endif
}
// Benchmark 2: Optional vs Raw Pointer Performance
template<typename T>
T* findElement_legacy(std::vector<T>& vec, const T& target) {
for (auto& item : vec) {
if (item == target) {
return &item;
}
}
return nullptr;
}
#if defined(ENABLE_CXX17) && __cplusplus >= 201703L
template<typename T>
std::optional<T*> findElement_modern(std::vector<T>& vec, const T& target) {
for (auto& item : vec) {
if (item == target) {
return &item;
}
}
return std::nullopt;
}
#endif
void benchmarkOptionalVsPointer() {
std::cout << "\n=== Optional vs Raw Pointer Benchmark ===\n";
std::vector<int> numbers;
for (int i = 0; i < 10000; ++i) {
numbers.push_back(i);
}
const int iterations = 1000;
// Legacy raw pointer approach
BenchTimer timer;
timer.start();
for (int iter = 0; iter < iterations; ++iter) {
int found_count = 0;
for (int search = 0; search < 100; ++search) {
int* found = findElement_legacy(numbers, search);
if (found) {
found_count++;
}
}
volatile int dummy = found_count;
(void)dummy;
}
double legacy_time = timer.stop();
#if defined(ENABLE_CXX17) && __cplusplus >= 201703L
// Modern optional approach
timer.start();
for (int iter = 0; iter < iterations; ++iter) {
int found_count = 0;
for (int search = 0; search < 100; ++search) {
auto found = findElement_modern(numbers, search);
if (found) {
found_count++;
}
}
volatile int dummy = found_count;
(void)dummy;
}
double modern_time = timer.stop();
double overhead = ((modern_time - legacy_time) / legacy_time) * 100.0;
std::cout << "Legacy (raw pointer): " << std::fixed << std::setprecision(2)
<< legacy_time / 1e6 << " ms\n";
std::cout << "Modern (std::optional): " << modern_time / 1e6 << " ms\n";
std::cout << "Overhead: " << overhead << "% (negative means faster)\n";
#else
std::cout << "Legacy (raw pointer): " << std::fixed << std::setprecision(2)
<< legacy_time / 1e6 << " ms\n";
std::cout << "Modern (std::optional): Not available (C++17 disabled)\n";
#endif
}
// Benchmark 3: Smart Pointer vs Raw Pointer Performance
void benchmarkSmartPointers() {
std::cout << "\n=== Smart Pointer vs Raw Pointer Benchmark ===\n";
const int iterations = 1000;
const int allocations = 100;
// Legacy raw pointer approach
BenchTimer timer;
timer.start();
for (int iter = 0; iter < iterations; ++iter) {
std::vector<std::vector<int>*> containers;
containers.reserve(allocations);
// Allocation
for (int i = 0; i < allocations; ++i) {
auto* container = new std::vector<int>(100, i);
containers.push_back(container);
}
// Usage simulation
size_t total_size = 0;
for (auto* container : containers) {
total_size += container->size();
}
// Manual cleanup
for (auto* container : containers) {
delete container;
}
volatile size_t dummy = total_size;
(void)dummy;
}
double legacy_time = timer.stop();
// Modern smart pointer approach
timer.start();
for (int iter = 0; iter < iterations; ++iter) {
std::vector<std::unique_ptr<std::vector<int>>> containers;
containers.reserve(allocations);
// Allocation
for (int i = 0; i < allocations; ++i) {
auto container = std::make_unique<std::vector<int>>(100, i);
containers.push_back(std::move(container));
}
// Usage simulation
size_t total_size = 0;
for (const auto& container : containers) {
total_size += container->size();
}
// Automatic cleanup when containers goes out of scope
volatile size_t dummy = total_size;
(void)dummy;
}
double modern_time = timer.stop();
double overhead = ((modern_time - legacy_time) / legacy_time) * 100.0;
std::cout << "Legacy (raw pointers): " << std::fixed << std::setprecision(2)
<< legacy_time / 1e6 << " ms\n";
std::cout << "Modern (unique_ptr): " << modern_time / 1e6 << " ms\n";
std::cout << "Overhead: " << overhead << "% (includes safety benefits)\n";
}
// Benchmark 4: Constexpr vs Runtime Calculation
constexpr size_t calculateSize_modern(size_t value) {
if (value < 253) return 1;
else if (value <= 0xFFFF) return 3;
else if (value <= 0xFFFFFFFF) return 5;
else return 9;
}
size_t calculateSize_legacy(size_t value) {
if (value < 253) return 1;
else if (value <= 0xFFFF) return 3;
else if (value <= 0xFFFFFFFF) return 5;
else return 9;
}
void benchmarkConstexprCalculation() {
std::cout << "\n=== Constexpr vs Runtime Calculation Benchmark ===\n";
std::vector<size_t> test_values = {0, 100, 252, 253, 65535, 65536, 4294967295ULL};
const int iterations = 100000;
// Legacy runtime calculation
BenchTimer timer;
timer.start();
for (int iter = 0; iter < iterations; ++iter) {
size_t total = 0;
for (auto value : test_values) {
total += calculateSize_legacy(value);
}
volatile size_t dummy = total;
(void)dummy;
}
double legacy_time = timer.stop();
// Modern constexpr calculation
timer.start();
for (int iter = 0; iter < iterations; ++iter) {
size_t total = 0;
for (auto value : test_values) {
total += calculateSize_modern(value);
}
volatile size_t dummy = total;
(void)dummy;
}
double modern_time = timer.stop();
double improvement = ((legacy_time - modern_time) / legacy_time) * 100.0;
std::cout << "Legacy (runtime): " << std::fixed << std::setprecision(2)
<< legacy_time / 1e6 << " ms\n";
std::cout << "Modern (constexpr): " << modern_time / 1e6 << " ms\n";
std::cout << "Improvement: " << improvement << "%\n";
}
int main() {
std::cout << "Modern C++ Migration Performance Benchmarks\n";
std::cout << "==========================================\n";
#if defined(ENABLE_CXX17) && __cplusplus >= 201703L
std::cout << "Compiled with C++17 support: ENABLED\n";
#else
std::cout << "Compiled with C++17 support: DISABLED\n";
#endif
benchmarkStringProcessing();
benchmarkOptionalVsPointer();
benchmarkSmartPointers();
benchmarkConstexprCalculation();
std::cout << "\nSummary:\n";
std::cout << "========\n";
std::cout << "- String operations with std::string_view show measurable improvements\n";
std::cout << "- std::optional has minimal overhead compared to raw pointers\n";
std::cout << "- Smart pointers have small overhead but provide significant safety benefits\n";
std::cout << "- constexpr functions enable compile-time optimizations\n";
std::cout << "\nThese benchmarks demonstrate that modern C++ features provide\n";
std::cout << "performance benefits while significantly improving code safety.\n";
return 0;
}