-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolyshape.cpp
More file actions
64 lines (49 loc) · 1.55 KB
/
polyshape.cpp
File metadata and controls
64 lines (49 loc) · 1.55 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
#include <benchmark/benchmark.h>
#include <memory>
#include <type_traits>
#include <utility>
#include "polymorphic_value.h"
#include "shapes.h"
#include "value_ptr.h"
template <typename Interface> class NonCopyableImplementation {
public:
template <typename Concrete, typename = std::enable_if_t<std::is_move_constructible_v<Concrete>>>
explicit NonCopyableImplementation(Concrete conc) : obj_(new Concrete(std::move(conc))) {}
constexpr auto operator->() const noexcept -> const Interface * { return obj_.get(); }
constexpr auto operator->() noexcept -> Interface * { return obj_.get(); }
private:
std::unique_ptr<Interface> obj_;
};
using Numeric = double;
static void BM_Virtual(benchmark::State &s) {
using Shape = NonCopyableImplementation<IShape<Numeric>>;
Shape shape(Rectangle(1.0, 1.0));
Numeric area = 0;
for (auto _ : s) {
area += shape->Area();
shape = std::move(shape);
}
benchmark::DoNotOptimize(area);
}
static void BM_PolyValue(benchmark::State &s) {
using Shape = nonstd::polymorphic_value<IShape<Numeric>>;
Shape shape(Rectangle(1.0, 1.0));
Numeric area = 0;
for (auto _ : s) {
area += shape->Area();
shape = std::move(shape);
}
benchmark::DoNotOptimize(area);
}
static void BM_ValuePtr(benchmark::State &s) {
auto shape = nonstd::make_polymorphic_value<IShape<Numeric>>(Rectangle(1.0, 1.0));
Numeric area = 0;
for (auto _ : s) {
area += shape->Area();
shape = std::move(shape);
}
benchmark::DoNotOptimize(area);
}
BENCHMARK(BM_Virtual);
BENCHMARK(BM_PolyValue);
BENCHMARK(BM_ValuePtr);