-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_uuid.cpp
97 lines (79 loc) · 2.28 KB
/
bench_uuid.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <benchmark/benchmark.h>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/thread/tss.hpp>
#include <boost/optional.hpp>
typedef boost::uuids::uuid UUID;
UUID random_generate() {
static boost::thread_specific_ptr<boost::uuids::random_generator> gen;
if (gen.get() == nullptr) {
gen.reset(new boost::uuids::random_generator);
}
return UUID((*gen)());
}
bool byRefUUID(const UUID& uid) {
bool result = uid > UUID();
return result;
}
bool byValUUID(UUID uid) {
bool result = uid > UUID();
return result;
}
static void BM_ByRefUUID(benchmark::State& state) {
UUID uid = random_generate();
for (auto _ : state) {
for (auto i = 0; i <= state.range(0); ++i) {
bool x = byRefUUID(uid);
benchmark::DoNotOptimize(x);
}
}
state.SetComplexityN(state.range(0));
}
static void BM_ByValUUID(benchmark::State& state) {
UUID uid = random_generate();
for (auto _ : state) {
for (auto i = 0; i <= state.range(0); ++i) {
bool x = byValUUID(uid);
benchmark::DoNotOptimize(x);
}
}
state.SetComplexityN(state.range(0));
}
// Register the function as a benchmark
BENCHMARK(BM_ByValUUID)->RangeMultiplier(2)->Range(8, 8 << 10)->Complexity();
BENCHMARK(BM_ByRefUUID)->RangeMultiplier(2)->Range(8, 8 << 10)->Complexity();
double byValOptional(boost::optional<double> x) {
if (x) {
return x.get();
}
return -999.0;
}
double byRefOptional(const boost::optional<double>& x) {
if (x) {
return x.get();
}
return -999.0;
}
static void BM_ByValOptional(benchmark::State& state) {
boost::optional<double> x_ = 999.0;
for (auto _ : state) {
for (auto i = 0; i <= state.range(0); ++i) {
double x = byValOptional(x_);
benchmark::DoNotOptimize(x);
}
}
state.SetComplexityN(state.range(0));
}
static void BM_ByRefOptional(benchmark::State& state) {
boost::optional<double> x_ = 999.0;
for (auto _ : state) {
for (auto i = 0; i <= state.range(0); ++i) {
double x = byRefOptional(x_);
benchmark::DoNotOptimize(x);
}
}
state.SetComplexityN(state.range(0));
}
BENCHMARK(BM_ByValOptional)->RangeMultiplier(2)->Range(8, 8 << 10)->Complexity();
BENCHMARK(BM_ByRefOptional)->RangeMultiplier(2)->Range(8, 8 << 10)->Complexity();