3 #include "GenerateInput.h"
4 #include "benchmark/benchmark.h"
5 #include "test_iterators.h"
7 namespace fs
= std::filesystem
;
9 static const size_t TestNumInputs
= 1024;
11 template <class GenInputs
>
12 void BM_PathConstructString(benchmark::State
& st
, GenInputs gen
) {
14 const auto in
= gen(st
.range(0));
18 benchmark::DoNotOptimize(PP
.native().data());
19 while (st
.KeepRunning()) {
20 const path
P(PP
.native());
21 benchmark::DoNotOptimize(P
.native().data());
23 st
.SetComplexityN(st
.range(0));
25 BENCHMARK_CAPTURE(BM_PathConstructString
, large_string
, getRandomStringInputs
)->Range(8, TestNumInputs
)->Complexity();
27 template <class GenInputs
>
28 void BM_PathConstructCStr(benchmark::State
& st
, GenInputs gen
) {
30 const auto in
= gen(st
.range(0));
34 benchmark::DoNotOptimize(PP
.native().data());
35 while (st
.KeepRunning()) {
36 const path
P(PP
.native().c_str());
37 benchmark::DoNotOptimize(P
.native().data());
40 BENCHMARK_CAPTURE(BM_PathConstructCStr
, large_string
, getRandomStringInputs
)->Arg(TestNumInputs
);
42 template <template <class...> class ItType
, class GenInputs
>
43 void BM_PathConstructIter(benchmark::State
& st
, GenInputs gen
) {
45 using Iter
= ItType
<std::string::const_iterator
>;
46 const auto in
= gen(st
.range(0));
50 auto Start
= Iter(PP
.native().begin());
51 auto End
= Iter(PP
.native().end());
52 benchmark::DoNotOptimize(PP
.native().data());
53 benchmark::DoNotOptimize(Start
);
54 benchmark::DoNotOptimize(End
);
55 while (st
.KeepRunning()) {
56 const path
P(Start
, End
);
57 benchmark::DoNotOptimize(P
.native().data());
59 st
.SetComplexityN(st
.range(0));
61 template <class GenInputs
>
62 void BM_PathConstructInputIter(benchmark::State
& st
, GenInputs gen
) {
63 BM_PathConstructIter
<cpp17_input_iterator
>(st
, gen
);
65 template <class GenInputs
>
66 void BM_PathConstructForwardIter(benchmark::State
& st
, GenInputs gen
) {
67 BM_PathConstructIter
<forward_iterator
>(st
, gen
);
69 BENCHMARK_CAPTURE(BM_PathConstructInputIter
, large_string
, getRandomStringInputs
)
70 ->Range(8, TestNumInputs
)
72 BENCHMARK_CAPTURE(BM_PathConstructForwardIter
, large_string
, getRandomStringInputs
)
73 ->Range(8, TestNumInputs
)
76 template <class GenInputs
>
77 void BM_PathIterateMultipleTimes(benchmark::State
& st
, GenInputs gen
) {
79 const auto in
= gen(st
.range(0));
83 benchmark::DoNotOptimize(PP
.native().data());
84 while (st
.KeepRunning()) {
85 for (auto const& E
: PP
) {
86 benchmark::DoNotOptimize(E
.native().data());
88 benchmark::ClobberMemory();
90 st
.SetComplexityN(st
.range(0));
92 BENCHMARK_CAPTURE(BM_PathIterateMultipleTimes
, iterate_elements
, getRandomStringInputs
)
93 ->Range(8, TestNumInputs
)
96 template <class GenInputs
>
97 void BM_PathIterateOnce(benchmark::State
& st
, GenInputs gen
) {
99 const auto in
= gen(st
.range(0));
101 for (auto& Part
: in
)
103 benchmark::DoNotOptimize(PP
.native().data());
104 while (st
.KeepRunning()) {
105 const path P
= PP
.native();
106 for (auto const& E
: P
) {
107 benchmark::DoNotOptimize(E
.native().data());
109 benchmark::ClobberMemory();
111 st
.SetComplexityN(st
.range(0));
113 BENCHMARK_CAPTURE(BM_PathIterateOnce
, iterate_elements
, getRandomStringInputs
)->Range(8, TestNumInputs
)->Complexity();
115 template <class GenInputs
>
116 void BM_PathIterateOnceBackwards(benchmark::State
& st
, GenInputs gen
) {
118 const auto in
= gen(st
.range(0));
120 for (auto& Part
: in
)
122 benchmark::DoNotOptimize(PP
.native().data());
123 while (st
.KeepRunning()) {
124 const path P
= PP
.native();
125 const auto B
= P
.begin();
129 benchmark::DoNotOptimize(*I
);
131 benchmark::DoNotOptimize(*I
);
134 BENCHMARK_CAPTURE(BM_PathIterateOnceBackwards
, iterate_elements
, getRandomStringInputs
)->Arg(TestNumInputs
);
136 static fs::path
getRandomPaths(int NumParts
, int PathLen
) {
139 std::string Part
= getRandomString(PathLen
);
145 template <class GenInput
>
146 void BM_LexicallyNormal(benchmark::State
& st
, GenInput gen
, size_t PathLen
) {
148 auto In
= gen(st
.range(0), PathLen
);
149 benchmark::DoNotOptimize(&In
);
150 while (st
.KeepRunning()) {
151 benchmark::DoNotOptimize(In
.lexically_normal());
153 st
.SetComplexityN(st
.range(0));
155 BENCHMARK_CAPTURE(BM_LexicallyNormal
, small_path
, getRandomPaths
, /*PathLen*/ 5)
159 BENCHMARK_CAPTURE(BM_LexicallyNormal
, large_path
, getRandomPaths
, /*PathLen*/ 32)