1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
13 #include "GenerateInput.h"
14 #include "benchmark/benchmark.h"
15 #include "test_iterators.h"
17 namespace fs
= std::filesystem
;
19 static const size_t TestNumInputs
= 1024;
21 template <class GenInputs
>
22 void BM_PathConstructString(benchmark::State
& st
, GenInputs gen
) {
24 const auto in
= gen(st
.range(0));
28 benchmark::DoNotOptimize(PP
.native().data());
29 while (st
.KeepRunning()) {
30 const path
P(PP
.native());
31 benchmark::DoNotOptimize(P
.native().data());
33 st
.SetComplexityN(st
.range(0));
35 BENCHMARK_CAPTURE(BM_PathConstructString
, large_string
, getRandomStringInputs
)->Range(8, TestNumInputs
)->Complexity();
37 template <class GenInputs
>
38 void BM_PathConstructCStr(benchmark::State
& st
, GenInputs gen
) {
40 const auto in
= gen(st
.range(0));
44 benchmark::DoNotOptimize(PP
.native().data());
45 while (st
.KeepRunning()) {
46 const path
P(PP
.native().c_str());
47 benchmark::DoNotOptimize(P
.native().data());
50 BENCHMARK_CAPTURE(BM_PathConstructCStr
, large_string
, getRandomStringInputs
)->Arg(TestNumInputs
);
52 template <template <class...> class ItType
, class GenInputs
>
53 void BM_PathConstructIter(benchmark::State
& st
, GenInputs gen
) {
55 using Iter
= ItType
<std::string::const_iterator
>;
56 const auto in
= gen(st
.range(0));
60 auto Start
= Iter(PP
.native().begin());
61 auto End
= Iter(PP
.native().end());
62 benchmark::DoNotOptimize(PP
.native().data());
63 benchmark::DoNotOptimize(Start
);
64 benchmark::DoNotOptimize(End
);
65 while (st
.KeepRunning()) {
66 const path
P(Start
, End
);
67 benchmark::DoNotOptimize(P
.native().data());
69 st
.SetComplexityN(st
.range(0));
71 template <class GenInputs
>
72 void BM_PathConstructInputIter(benchmark::State
& st
, GenInputs gen
) {
73 BM_PathConstructIter
<cpp17_input_iterator
>(st
, gen
);
75 template <class GenInputs
>
76 void BM_PathConstructForwardIter(benchmark::State
& st
, GenInputs gen
) {
77 BM_PathConstructIter
<forward_iterator
>(st
, gen
);
79 BENCHMARK_CAPTURE(BM_PathConstructInputIter
, large_string
, getRandomStringInputs
)
80 ->Range(8, TestNumInputs
)
82 BENCHMARK_CAPTURE(BM_PathConstructForwardIter
, large_string
, getRandomStringInputs
)
83 ->Range(8, TestNumInputs
)
86 template <class GenInputs
>
87 void BM_PathIterateMultipleTimes(benchmark::State
& st
, GenInputs gen
) {
89 const auto in
= gen(st
.range(0));
93 benchmark::DoNotOptimize(PP
.native().data());
94 while (st
.KeepRunning()) {
95 for (auto const& E
: PP
) {
96 benchmark::DoNotOptimize(E
.native().data());
98 benchmark::ClobberMemory();
100 st
.SetComplexityN(st
.range(0));
102 BENCHMARK_CAPTURE(BM_PathIterateMultipleTimes
, iterate_elements
, getRandomStringInputs
)
103 ->Range(8, TestNumInputs
)
106 template <class GenInputs
>
107 void BM_PathIterateOnce(benchmark::State
& st
, GenInputs gen
) {
109 const auto in
= gen(st
.range(0));
111 for (auto& Part
: in
)
113 benchmark::DoNotOptimize(PP
.native().data());
114 while (st
.KeepRunning()) {
115 const path P
= PP
.native();
116 for (auto const& E
: P
) {
117 benchmark::DoNotOptimize(E
.native().data());
119 benchmark::ClobberMemory();
121 st
.SetComplexityN(st
.range(0));
123 BENCHMARK_CAPTURE(BM_PathIterateOnce
, iterate_elements
, getRandomStringInputs
)->Range(8, TestNumInputs
)->Complexity();
125 template <class GenInputs
>
126 void BM_PathIterateOnceBackwards(benchmark::State
& st
, GenInputs gen
) {
128 const auto in
= gen(st
.range(0));
130 for (auto& Part
: in
)
132 benchmark::DoNotOptimize(PP
.native().data());
133 while (st
.KeepRunning()) {
134 const path P
= PP
.native();
135 const auto B
= P
.begin();
139 benchmark::DoNotOptimize(*I
);
141 benchmark::DoNotOptimize(*I
);
144 BENCHMARK_CAPTURE(BM_PathIterateOnceBackwards
, iterate_elements
, getRandomStringInputs
)->Arg(TestNumInputs
);
146 static fs::path
getRandomPaths(int NumParts
, int PathLen
) {
149 std::string Part
= getRandomString(PathLen
);
155 template <class GenInput
>
156 void BM_LexicallyNormal(benchmark::State
& st
, GenInput gen
, size_t PathLen
) {
158 auto In
= gen(st
.range(0), PathLen
);
159 benchmark::DoNotOptimize(&In
);
160 while (st
.KeepRunning()) {
161 benchmark::DoNotOptimize(In
.lexically_normal());
163 st
.SetComplexityN(st
.range(0));
165 BENCHMARK_CAPTURE(BM_LexicallyNormal
, small_path
, getRandomPaths
, /*PathLen*/ 5)
169 BENCHMARK_CAPTURE(BM_LexicallyNormal
, large_path
, getRandomPaths
, /*PathLen*/ 32)