llvm-project
164 строки · 4.8 Кб
1#include <filesystem>2
3#include "GenerateInput.h"4#include "benchmark/benchmark.h"5#include "test_iterators.h"6
7namespace fs = std::filesystem;8
9static const size_t TestNumInputs = 1024;10
11template <class GenInputs>12void BM_PathConstructString(benchmark::State& st, GenInputs gen) {13using fs::path;14const auto in = gen(st.range(0));15path PP;16for (auto& Part : in)17PP /= Part;18benchmark::DoNotOptimize(PP.native().data());19while (st.KeepRunning()) {20const path P(PP.native());21benchmark::DoNotOptimize(P.native().data());22}23st.SetComplexityN(st.range(0));24}
25BENCHMARK_CAPTURE(BM_PathConstructString, large_string, getRandomStringInputs)->Range(8, TestNumInputs)->Complexity();26
27template <class GenInputs>28void BM_PathConstructCStr(benchmark::State& st, GenInputs gen) {29using fs::path;30const auto in = gen(st.range(0));31path PP;32for (auto& Part : in)33PP /= Part;34benchmark::DoNotOptimize(PP.native().data());35while (st.KeepRunning()) {36const path P(PP.native().c_str());37benchmark::DoNotOptimize(P.native().data());38}39}
40BENCHMARK_CAPTURE(BM_PathConstructCStr, large_string, getRandomStringInputs)->Arg(TestNumInputs);41
42template <template <class...> class ItType, class GenInputs>43void BM_PathConstructIter(benchmark::State& st, GenInputs gen) {44using fs::path;45using Iter = ItType<std::string::const_iterator>;46const auto in = gen(st.range(0));47path PP;48for (auto& Part : in)49PP /= Part;50auto Start = Iter(PP.native().begin());51auto End = Iter(PP.native().end());52benchmark::DoNotOptimize(PP.native().data());53benchmark::DoNotOptimize(Start);54benchmark::DoNotOptimize(End);55while (st.KeepRunning()) {56const path P(Start, End);57benchmark::DoNotOptimize(P.native().data());58}59st.SetComplexityN(st.range(0));60}
61template <class GenInputs>62void BM_PathConstructInputIter(benchmark::State& st, GenInputs gen) {63BM_PathConstructIter<cpp17_input_iterator>(st, gen);64}
65template <class GenInputs>66void BM_PathConstructForwardIter(benchmark::State& st, GenInputs gen) {67BM_PathConstructIter<forward_iterator>(st, gen);68}
69BENCHMARK_CAPTURE(BM_PathConstructInputIter, large_string, getRandomStringInputs)70->Range(8, TestNumInputs)71->Complexity();72BENCHMARK_CAPTURE(BM_PathConstructForwardIter, large_string, getRandomStringInputs)73->Range(8, TestNumInputs)74->Complexity();75
76template <class GenInputs>77void BM_PathIterateMultipleTimes(benchmark::State& st, GenInputs gen) {78using fs::path;79const auto in = gen(st.range(0));80path PP;81for (auto& Part : in)82PP /= Part;83benchmark::DoNotOptimize(PP.native().data());84while (st.KeepRunning()) {85for (auto const& E : PP) {86benchmark::DoNotOptimize(E.native().data());87}88benchmark::ClobberMemory();89}90st.SetComplexityN(st.range(0));91}
92BENCHMARK_CAPTURE(BM_PathIterateMultipleTimes, iterate_elements, getRandomStringInputs)93->Range(8, TestNumInputs)94->Complexity();95
96template <class GenInputs>97void BM_PathIterateOnce(benchmark::State& st, GenInputs gen) {98using fs::path;99const auto in = gen(st.range(0));100path PP;101for (auto& Part : in)102PP /= Part;103benchmark::DoNotOptimize(PP.native().data());104while (st.KeepRunning()) {105const path P = PP.native();106for (auto const& E : P) {107benchmark::DoNotOptimize(E.native().data());108}109benchmark::ClobberMemory();110}111st.SetComplexityN(st.range(0));112}
113BENCHMARK_CAPTURE(BM_PathIterateOnce, iterate_elements, getRandomStringInputs)->Range(8, TestNumInputs)->Complexity();114
115template <class GenInputs>116void BM_PathIterateOnceBackwards(benchmark::State& st, GenInputs gen) {117using fs::path;118const auto in = gen(st.range(0));119path PP;120for (auto& Part : in)121PP /= Part;122benchmark::DoNotOptimize(PP.native().data());123while (st.KeepRunning()) {124const path P = PP.native();125const auto B = P.begin();126auto I = P.end();127while (I != B) {128--I;129benchmark::DoNotOptimize(*I);130}131benchmark::DoNotOptimize(*I);132}133}
134BENCHMARK_CAPTURE(BM_PathIterateOnceBackwards, iterate_elements, getRandomStringInputs)->Arg(TestNumInputs);135
136static fs::path getRandomPaths(int NumParts, int PathLen) {137fs::path Result;138while (NumParts--) {139std::string Part = getRandomString(PathLen);140Result /= Part;141}142return Result;143}
144
145template <class GenInput>146void BM_LexicallyNormal(benchmark::State& st, GenInput gen, size_t PathLen) {147using fs::path;148auto In = gen(st.range(0), PathLen);149benchmark::DoNotOptimize(&In);150while (st.KeepRunning()) {151benchmark::DoNotOptimize(In.lexically_normal());152}153st.SetComplexityN(st.range(0));154}
155BENCHMARK_CAPTURE(BM_LexicallyNormal, small_path, getRandomPaths, /*PathLen*/ 5)156->RangeMultiplier(2)157->Range(2, 256)158->Complexity();159BENCHMARK_CAPTURE(BM_LexicallyNormal, large_path, getRandomPaths, /*PathLen*/ 32)160->RangeMultiplier(2)161->Range(2, 256)162->Complexity();163
164BENCHMARK_MAIN();165