-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrtp.cpp
More file actions
56 lines (46 loc) · 1.14 KB
/
crtp.cpp
File metadata and controls
56 lines (46 loc) · 1.14 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
#include <benchmark/benchmark.h>
namespace crtp {
template <typename T>
class Base {
public:
int interface() { return static_cast<T*>(this)->impl(); }
};
class Derived : public Base<Derived> {
public:
int impl() {
// for (int i = 0; i < 100; ++i) { asm(""); }
return 0;
}
};
} // namespace crtp
namespace vtable {
class Base {
public:
virtual int interface() = 0;
virtual ~Base() {}
};
class Derived : public Base {
public:
int interface() override {
// for (int i = 0; i < 100; ++i) { asm(""); }
return 0;
}
};
} // namespace vtable
crtp::Base<crtp::Derived> *crtp_obj = new crtp::Derived();
static void __attribute__ ((noinline)) BM_CrtpCall(benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(crtp_obj->interface());
}
}
// Register the function as a benchmark
BENCHMARK(BM_CrtpCall);
vtable::Base *vtable_obj = new vtable::Derived();
static void __attribute__ ((noinline)) BM_VtableCall(benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(vtable_obj->interface());
}
}
// Register the function as a benchmark
BENCHMARK(BM_VtableCall);
BENCHMARK_MAIN();