-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_parallel_runner.cpp
More file actions
99 lines (83 loc) · 2.91 KB
/
benchmark_parallel_runner.cpp
File metadata and controls
99 lines (83 loc) · 2.91 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
#include <chrono>
#include <iomanip>
#include <iostream>
#include "parallel_runner.hpp"
// Benchmark helper
template <typename Func>
double benchmark(const char* name, Func&& func, int iterations = 1000000) {
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; ++i) {
func();
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
double avg_ns = static_cast<double>(duration) / iterations;
std::cout << std::setw(40) << std::left << name << std::setw(12) << std::right << std::fixed
<< std::setprecision(2) << avg_ns << " ns/iteration\n";
return avg_ns;
}
int main() {
std::cout << "ParallelRunner Performance Benchmark\n";
std::cout << "======================================\n\n";
// Simple lambdas with no captures
auto runner = make_parallel_runner(
[]() { return true; }, "Step 1 failed", []() { return true; }, "Step 2 failed",
[]() { return true; }, "Step 3 failed", []() { return true; }, "Step 4 failed",
[]() { return true; }, "Step 5 failed");
// Lambdas with captures
int counter = 0;
auto runner_with_captures = make_parallel_runner(
[&counter]() {
counter++;
return true;
},
"Step 1 failed",
[&counter]() {
counter++;
return true;
},
"Step 2 failed",
[&counter]() {
counter++;
return true;
},
"Step 3 failed",
[&counter]() {
counter++;
return true;
},
"Step 4 failed",
[&counter]() {
counter++;
return true;
},
"Step 5 failed");
std::cout << "Test 1: Simple lambdas (5 steps, all succeed)\n";
benchmark(" run()", [&]() { runner.run(); });
std::cout << "\nTest 2: Lambdas with captures (5 steps, all succeed)\n";
benchmark(" run()", [&]() { runner_with_captures.run(); });
std::cout << "\nTest 3: Query operations\n";
runner.run();
benchmark(" result(index)", [&]() {
auto res = runner.result(2);
(void)res;
});
benchmark(" error_message(index)", [&]() {
auto msg = runner.error_message(2);
(void)msg;
});
benchmark(" all_succeeded()", [&]() {
auto res = runner.all_succeeded();
(void)res;
});
benchmark(" success_count()", [&]() {
auto count = runner.success_count();
(void)count;
});
benchmark(" rerun(index)", [&]() { runner.rerun(0); });
std::cout << "\n✅ Zero std::function overhead!\n";
std::cout << " Each lambda is stored with its actual type (no type erasure)\n";
std::cout << " No heap allocations during construction or execution\n";
std::cout << " All results stored in fixed-size std::array\n";
return 0;
}