-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisit.cpp
More file actions
98 lines (80 loc) · 2.24 KB
/
visit.cpp
File metadata and controls
98 lines (80 loc) · 2.24 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
#include <benchmark/benchmark.h>
#include "shapes.h"
#include "visit.h"
using Number = double;
template <typename Numeric, std::size_t I>
struct DummyShape : public AbstractShape<Numeric, DummyShape<Numeric, I>> {
[[nodiscard]] constexpr auto compute_area() const -> Numeric { return I; }
};
using Shape = std::variant<DummyShape<Number, 0>, DummyShape<Number, 1>, DummyShape<Number, 2>,
DummyShape<Number, 3>, DummyShape<Number, 4>, DummyShape<Number, 5>,
DummyShape<Number, 6>, DummyShape<Number, 7>, DummyShape<Number, 8>,
DummyShape<Number, 9>>;
static constexpr auto DIMENSION1 = 5.0;
static constexpr auto DIMENSION2 = 4.0;
struct ShapeUpdater {
static constexpr auto REUSE_COUNT = 100;
explicit ShapeUpdater(Shape &shape) : shape(shape) {}
void operator()() noexcept {
if (++count % REUSE_COUNT == 0) {
switch (++next_shape % 3) {
case 0:
shape = DummyShape<Number, 0>{};
break;
case 1:
shape = DummyShape<Number, 1>{};
break;
case 2:
shape = DummyShape<Number, 2>{};
break;
case 3:
shape = DummyShape<Number, 3>{};
break;
case 4:
shape = DummyShape<Number, 4>{};
break;
case 5:
shape = DummyShape<Number, 5>{};
break;
case 6:
shape = DummyShape<Number, 6>{};
break;
case 7:
shape = DummyShape<Number, 7>{};
break;
case 8:
shape = DummyShape<Number, 8>{};
break;
case 9:
shape = DummyShape<Number, 9>{};
break;
}
}
}
private:
int count = 0;
int next_shape = 0;
Shape &shape;
};
static void BM_StdVisit(benchmark::State &s) {
Number area = 0;
Shape shape;
ShapeUpdater updater(shape);
for (auto _ : s) {
area += std::visit([](auto &s) { return s.Area(); }, shape);
updater();
}
benchmark::DoNotOptimize(area);
}
static void BM_MyVisit(benchmark::State &s) {
Number area = 0;
Shape shape;
ShapeUpdater updater(shape);
for (auto _ : s) {
area += nonstd::visit([](auto &s) { return s.Area(); }, shape);
updater();
}
benchmark::DoNotOptimize(area);
}
BENCHMARK(BM_StdVisit);
BENCHMARK(BM_MyVisit);