Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / benchmarks / filesystem.bench.cpp
blob44200fb3cab08bd4a25d542a6698004be7631db9
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) {
10 using fs::path;
11 const auto in = gen(st.range(0));
12 path PP;
13 for (auto& Part : in)
14 PP /= Part;
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) {
26 using fs::path;
27 const auto in = gen(st.range(0));
28 path PP;
29 for (auto& Part : in)
30 PP /= Part;
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) {
41 using fs::path;
42 using Iter = ItType<std::string::const_iterator>;
43 const auto in = gen(st.range(0));
44 path PP;
45 for (auto& Part : in)
46 PP /= Part;
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)
68 ->Complexity();
69 BENCHMARK_CAPTURE(BM_PathConstructForwardIter, large_string, getRandomStringInputs)
70 ->Range(8, TestNumInputs)
71 ->Complexity();
73 template <class GenInputs>
74 void BM_PathIterateMultipleTimes(benchmark::State& st, GenInputs gen) {
75 using fs::path;
76 const auto in = gen(st.range(0));
77 path PP;
78 for (auto& Part : in)
79 PP /= Part;
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)
91 ->Complexity();
93 template <class GenInputs>
94 void BM_PathIterateOnce(benchmark::State& st, GenInputs gen) {
95 using fs::path;
96 const auto in = gen(st.range(0));
97 path PP;
98 for (auto& Part : in)
99 PP /= Part;
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) {
114 using fs::path;
115 const auto in = gen(st.range(0));
116 path PP;
117 for (auto& Part : in)
118 PP /= Part;
119 benchmark::DoNotOptimize(PP.native().data());
120 while (st.KeepRunning()) {
121 const path P = PP.native();
122 const auto B = P.begin();
123 auto I = P.end();
124 while (I != B) {
125 --I;
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) {
134 fs::path Result;
135 while (NumParts--) {
136 std::string Part = getRandomString(PathLen);
137 Result /= Part;
139 return Result;
142 template <class GenInput>
143 void BM_LexicallyNormal(benchmark::State& st, GenInput gen, size_t PathLen) {
144 using fs::path;
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)
153 ->RangeMultiplier(2)
154 ->Range(2, 256)
155 ->Complexity();
156 BENCHMARK_CAPTURE(BM_LexicallyNormal, large_path, getRandomPaths, /*PathLen*/ 32)
157 ->RangeMultiplier(2)
158 ->Range(2, 256)
159 ->Complexity();
161 BENCHMARK_MAIN();