-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark.cpp
More file actions
205 lines (180 loc) · 5.64 KB
/
benchmark.cpp
File metadata and controls
205 lines (180 loc) · 5.64 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
#include <benchmark/benchmark.h>
#include <algorithm>
#ifdef USE_BOOST_LOCKLESS_Q
#include "cptl.hpp"
#else
#include "cptl_stl.hpp"
#endif
#include "graphex.hpp"
using namespace GE;
std::function<void(void)> firstFunc = []() -> void {};
std::function<int(void)> secondFunc = []() -> int { return 1; };
std::function<int(int)> thirdFunc = [](int a) -> int { return a + 2; };
std::function<int(int)> fourthFunc = [](int a) -> int { return a * 2; };
std::function<int(int, int)> fifthFunc = [](int a, int b) -> int {
return a % b;
};
static void BM_GraphEX(benchmark::State& state)
{
for (auto _ : state) {
GraphEx executor;
decltype(auto) first = executor.makeNode(firstFunc);
decltype(auto) second = executor.makeNode(secondFunc);
decltype(auto) third = executor.makeNode(thirdFunc);
decltype(auto) fourth = executor.makeNode(fourthFunc);
decltype(auto) fifth = executor.makeNode(fifthFunc);
second->setParent(first);
third->setParent<0>(second);
fourth->setParent<0>(second);
fifth->setParent<0>(third);
fifth->setParent<1>(fourth);
executor.execute();
}
}
// Register the function as a benchmark
BENCHMARK(BM_GraphEX);
// Define another benchmark
static void BM_FunctionCall(benchmark::State& state)
{
for (auto _ : state) {
firstFunc();
auto res = secondFunc();
auto a = thirdFunc(res);
auto b = thirdFunc(res);
fifthFunc(a, b);
}
}
BENCHMARK(BM_FunctionCall);
constexpr int loop_n = 1'000'000;
std::function<void(void)> firstCostlyFunc = []() -> void {
for (int i = 0; i < loop_n; ++i)
;
};
std::function<int(void)> secondCostlyFunc = []() -> int {
int k = 1;
for (int i = 0; i < loop_n; ++i)
k ^= i;
return k;
};
std::function<int(int)> thirdCostlyFunc = [](int a) -> int {
for (int i = loop_n; i >= 0; --i) {
if (i & 1)
a = std::min(a ^ i, i + 10);
}
return a;
};
std::function<int(int)> fourthCostlyFunc = [](int a) -> int {
for (int i = 1'000; i >= 0; --i) {
for (int j = 1; j <= 1'000; ++j) {
a ^= (i % j);
++a;
}
}
return a;
};
std::function<int(int, int)> fifthCostlyFunc = [](int a, int b) -> int {
int ret = 1;
constexpr int MOD = 1e9 + 7;
b = std::abs(b);
while (b) {
if (b & 1)
ret = (long long)ret * a % MOD;
a = (long long)a * a % MOD;
b >>= 1;
}
return ret;
};
auto sixCostlyFunc(int a, int b, int c, int d) -> int
{
a = std::max(a, c);
b = std::max(b, d);
int ret = 1;
constexpr int MOD = 1e9 + 7;
b = std::abs(b);
while (b) {
if (b & 1)
ret = (long long)ret * a % MOD;
a = (long long)a * a % MOD;
b >>= 1;
}
return ret;
}
static void BM_GraphEX_Expensive(benchmark::State& state)
{
for (auto _ : state) {
GraphEx executor;
decltype(auto) first = executor.makeNode(firstCostlyFunc);
decltype(auto) second = executor.makeNode(secondCostlyFunc);
decltype(auto) third = executor.makeNode(thirdCostlyFunc);
decltype(auto) fourth = executor.makeNode(fourthCostlyFunc);
decltype(auto) fifth = executor.makeNode(fifthCostlyFunc);
second->setParent(first);
third->setParent<0>(second);
fourth->setParent<0>(second);
fifth->setParent<0>(third);
fifth->setParent<1>(fourth);
executor.execute();
}
}
BENCHMARK(BM_GraphEX_Expensive);
static void BM_FunctionCall_Expensive(benchmark::State& state)
{
for (auto _ : state) {
firstCostlyFunc();
auto res = secondCostlyFunc();
auto a = thirdCostlyFunc(res);
auto b = fourthCostlyFunc(res);
fifthCostlyFunc(a, b);
}
}
BENCHMARK(BM_FunctionCall_Expensive);
static void BM_GraphEX_Expensive_Parallel(benchmark::State& state)
{
GraphEx executor(4);
decltype(auto) first = executor.makeNode(secondCostlyFunc);
decltype(auto) second = executor.makeNode(thirdCostlyFunc);
decltype(auto) third = executor.makeNode(thirdCostlyFunc);
decltype(auto) fourth = executor.makeNode(fourthCostlyFunc);
decltype(auto) fifth = executor.makeNode(fourthCostlyFunc);
std::function<int(int, int, int, int)> ss = sixCostlyFunc;
decltype(auto) sixth = executor.makeNode(ss);
second->setParent<0>(first);
third->setParent<0>(first);
fourth->setParent<0>(first);
fifth->setParent<0>(first);
sixth->setParent<0>(second);
sixth->setParent<1>(third);
sixth->setParent<2>(fourth);
sixth->setParent<3>(fifth);
for (auto _ : state) {
executor.execute();
executor.reset();
}
}
BENCHMARK(BM_GraphEX_Expensive_Parallel);
static void BM_FunctionCall_Expensive_NonParallel(benchmark::State& state)
{
for (auto _ : state) {
auto res1 = secondCostlyFunc();
auto res2 = thirdCostlyFunc(res1);
auto res3 = thirdCostlyFunc(res1);
auto res4 = fourthCostlyFunc(res1);
auto res5 = fourthCostlyFunc(res1);
sixCostlyFunc(res2, res3, res4, res5);
}
}
BENCHMARK(BM_FunctionCall_Expensive_NonParallel);
static void BM_FunctionCall_Expensive_Parallel(benchmark::State& state)
{
ctpl::thread_pool pool(4);
for (auto _ : state) {
auto res = secondCostlyFunc();
auto f1 = pool.push(std::bind(thirdCostlyFunc, res));
auto f2 = pool.push(std::bind(thirdCostlyFunc, res));
auto f3 = pool.push(std::bind(fourthCostlyFunc, res));
auto f4 = pool.push(std::bind(fourthCostlyFunc, res));
sixCostlyFunc(f1.get(), f2.get(), f3.get(), f4.get());
}
}
BENCHMARK(BM_FunctionCall_Expensive_Parallel);
BENCHMARK_MAIN();