-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_shared_ptr.cpp
79 lines (62 loc) · 1.41 KB
/
bench_shared_ptr.cpp
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
#include <benchmark/benchmark.h>
#include <vector>
#include <memory>
#include <string>
class X_Impl
{
public:
[[nodiscard]] std::string get_i() const {
return i;
};
void set_i(const std::string& new_i) {
i = new_i;
};
private:
std::string i;
std::string j;
std::string k;
std::string l;
};
class X
{
public:
X() : impl{std::make_shared<X_Impl>()} {}
[[nodiscard]] std::string get_i() const {
return impl->get_i();
}
void set_i(const std::string& new_i) const {
return impl->set_i(new_i);
}
private:
std::shared_ptr<X_Impl> impl;
};
void passByValue(X x) {
return x.set_i("new string is assigned");
}
void passByRef(X& x) {
return x.set_i("new string is assigned");
}
static void BenchByVal(benchmark::State& state) {
X x;
// Code inside this loop is measured repeatedly
for (auto _ : state) {
for (int64_t i = 0; i < state.range(0); ++i) {
passByValue(x);
}
}
state.SetComplexityN(state.range(0));
}
// Register the function as a benchmark
BENCHMARK(BenchByVal)->Range(8, 8 << 10)->Complexity();
static void BenchByRef(benchmark::State& state) {
X x;
// Code inside this loop is measured repeatedly
for (auto _ : state) {
for (int64_t i = 0; i < state.range(0); ++i) {
passByRef(x);
}
}
state.SetComplexityN(state.range(0));
}
// Register the function as a benchmark
BENCHMARK(BenchByRef)->Range(8, 8 << 10)->Complexity();