1 #include "GenerateInput.h"
2 #include "benchmark/benchmark.h"
3 #include "filesystem_include.h"
4 #include "test_iterators.h"
6 static const size_t TestNumInputs
= 1024;
8 template <class GenInputs
>
9 void BM_PathConstructString(benchmark::State
& st
, GenInputs gen
) {
11 const auto in
= gen(st
.range(0));
15 benchmark::DoNotOptimize(PP
.native().data());
16 while (st
.KeepRunning()) {
17 const path
P(PP
.native());
18 benchmark::DoNotOptimize(P
.native().data());
20 st
.SetComplexityN(st
.range(0));
22 BENCHMARK_CAPTURE(BM_PathConstructString
, large_string
, getRandomStringInputs
)->Range(8, TestNumInputs
)->Complexity();
24 template <class GenInputs
>
25 void BM_PathConstructCStr(benchmark::State
& st
, GenInputs gen
) {
27 const auto in
= gen(st
.range(0));
31 benchmark::DoNotOptimize(PP
.native().data());
32 while (st
.KeepRunning()) {
33 const path
P(PP
.native().c_str());
34 benchmark::DoNotOptimize(P
.native().data());
37 BENCHMARK_CAPTURE(BM_PathConstructCStr
, large_string
, getRandomStringInputs
)->Arg(TestNumInputs
);
39 template <template <class...> class ItType
, class GenInputs
>
40 void BM_PathConstructIter(benchmark::State
& st
, GenInputs gen
) {
42 using Iter
= ItType
<std::string::const_iterator
>;
43 const auto in
= gen(st
.range(0));
47 auto Start
= Iter(PP
.native().begin());
48 auto End
= Iter(PP
.native().end());
49 benchmark::DoNotOptimize(PP
.native().data());
50 benchmark::DoNotOptimize(Start
);
51 benchmark::DoNotOptimize(End
);
52 while (st
.KeepRunning()) {
53 const path
P(Start
, End
);
54 benchmark::DoNotOptimize(P
.native().data());
56 st
.SetComplexityN(st
.range(0));
58 template <class GenInputs
>
59 void BM_PathConstructInputIter(benchmark::State
& st
, GenInputs gen
) {
60 BM_PathConstructIter
<cpp17_input_iterator
>(st
, gen
);
62 template <class GenInputs
>
63 void BM_PathConstructForwardIter(benchmark::State
& st
, GenInputs gen
) {
64 BM_PathConstructIter
<forward_iterator
>(st
, gen
);
66 BENCHMARK_CAPTURE(BM_PathConstructInputIter
, large_string
, getRandomStringInputs
)
67 ->Range(8, TestNumInputs
)
69 BENCHMARK_CAPTURE(BM_PathConstructForwardIter
, large_string
, getRandomStringInputs
)
70 ->Range(8, TestNumInputs
)
73 template <class GenInputs
>
74 void BM_PathIterateMultipleTimes(benchmark::State
& st
, GenInputs gen
) {
76 const auto in
= gen(st
.range(0));
80 benchmark::DoNotOptimize(PP
.native().data());
81 while (st
.KeepRunning()) {
82 for (auto const& E
: PP
) {
83 benchmark::DoNotOptimize(E
.native().data());
85 benchmark::ClobberMemory();
87 st
.SetComplexityN(st
.range(0));
89 BENCHMARK_CAPTURE(BM_PathIterateMultipleTimes
, iterate_elements
, getRandomStringInputs
)
90 ->Range(8, TestNumInputs
)
93 template <class GenInputs
>
94 void BM_PathIterateOnce(benchmark::State
& st
, GenInputs gen
) {
96 const auto in
= gen(st
.range(0));
100 benchmark::DoNotOptimize(PP
.native().data());
101 while (st
.KeepRunning()) {
102 const path P
= PP
.native();
103 for (auto const& E
: P
) {
104 benchmark::DoNotOptimize(E
.native().data());
106 benchmark::ClobberMemory();
108 st
.SetComplexityN(st
.range(0));
110 BENCHMARK_CAPTURE(BM_PathIterateOnce
, iterate_elements
, getRandomStringInputs
)->Range(8, TestNumInputs
)->Complexity();
112 template <class GenInputs
>
113 void BM_PathIterateOnceBackwards(benchmark::State
& st
, GenInputs gen
) {
115 const auto in
= gen(st
.range(0));
117 for (auto& Part
: in
)
119 benchmark::DoNotOptimize(PP
.native().data());
120 while (st
.KeepRunning()) {
121 const path P
= PP
.native();
122 const auto B
= P
.begin();
126 benchmark::DoNotOptimize(*I
);
128 benchmark::DoNotOptimize(*I
);
131 BENCHMARK_CAPTURE(BM_PathIterateOnceBackwards
, iterate_elements
, getRandomStringInputs
)->Arg(TestNumInputs
);
133 static fs::path
getRandomPaths(int NumParts
, int PathLen
) {
136 std::string Part
= getRandomString(PathLen
);
142 template <class GenInput
>
143 void BM_LexicallyNormal(benchmark::State
& st
, GenInput gen
, size_t PathLen
) {
145 auto In
= gen(st
.range(0), PathLen
);
146 benchmark::DoNotOptimize(&In
);
147 while (st
.KeepRunning()) {
148 benchmark::DoNotOptimize(In
.lexically_normal());
150 st
.SetComplexityN(st
.range(0));
152 BENCHMARK_CAPTURE(BM_LexicallyNormal
, small_path
, getRandomPaths
, /*PathLen*/ 5)
156 BENCHMARK_CAPTURE(BM_LexicallyNormal
, large_path
, getRandomPaths
, /*PathLen*/ 32)