-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_PassStringVal_vs_Ref.cpp
58 lines (43 loc) · 1.2 KB
/
bench_PassStringVal_vs_Ref.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
#include <benchmark/benchmark.h>
#include <string>
namespace current {
bool passX_Impl([[maybe_unused]] std::string x) {
return true;
}
bool passX(std::string x) {
return passX_Impl(x);
}
}
static void StringByVal(benchmark::State& state) {
// std::string testString = "Air Terminal Single Duct Constant Volume No Reheat 1";
// std::string s(5, 'a'); // s == "aaaaa"
std::string testString(state.range(0), 'a');
for (auto _ : state) {
bool ret = current::passX(testString);
benchmark::DoNotOptimize(ret);
}
state.SetComplexityN(state.range(0));
}
namespace proposed {
bool passX_Impl([[maybe_unused]] const std::string& x) {
return true;
}
bool passX(const std::string& x) {
return passX_Impl(x);
}
}
static void StringByRef(benchmark::State& state) {
// std::string testString = "Air Terminal Single Duct Constant Volume No Reheat 1";
std::string testString(state.range(0), 'a');
for (auto _ : state) {
bool ret = proposed::passX(testString);
benchmark::DoNotOptimize(ret);
}
state.SetComplexityN(state.range(0));
}
BENCHMARK(StringByVal)
->Range(8, 8<<10)
->Complexity();
BENCHMARK(StringByRef)
->Range(8, 8<<10)
->Complexity();