1 #include <experimental/filesystem> 2 3 #include "benchmark/benchmark.h" 4 #include "GenerateInput.hpp" 5 #include "test_iterators.h" 6 7 namespace fs = std::experimental::filesystem; 8 9 static const size_t TestNumInputs = 1024; 10 11 12 template <class GenInputs> 13 void BM_PathConstructString(benchmark::State &st, GenInputs gen) { 14 using namespace fs; 15 const auto in = gen(st.range(0)); 16 path PP; 17 for (auto& Part : in) 18 PP /= Part; 19 benchmark::DoNotOptimize(PP.native().data()); 20 while (st.KeepRunning()) { 21 const path P(PP.native()); 22 benchmark::DoNotOptimize(P.native().data()); 23 } 24 } 25 BENCHMARK_CAPTURE(BM_PathConstructString, large_string, 26 getRandomStringInputs)->Arg(TestNumInputs); 27 28 29 template <class GenInputs> 30 void BM_PathConstructCStr(benchmark::State &st, GenInputs gen) { 31 using namespace fs; 32 const auto in = gen(st.range(0)); 33 path PP; 34 for (auto& Part : in) 35 PP /= Part; 36 benchmark::DoNotOptimize(PP.native().data()); 37 while (st.KeepRunning()) { 38 const path P(PP.native().c_str()); 39 benchmark::DoNotOptimize(P.native().data()); 40 } 41 } 42 BENCHMARK_CAPTURE(BM_PathConstructCStr, large_string, 43 getRandomStringInputs)->Arg(TestNumInputs); 44 45 46 template <template <class...> class ItType, class GenInputs> 47 void BM_PathConstructIter(benchmark::State &st, GenInputs gen) { 48 using namespace fs; 49 using Iter = ItType<std::string::const_iterator>; 50 const auto in = gen(st.range(0)); 51 path PP; 52 for (auto& Part : in) 53 PP /= Part; 54 auto Start = Iter(PP.native().begin()); 55 auto End = Iter(PP.native().end()); 56 benchmark::DoNotOptimize(PP.native().data()); 57 benchmark::DoNotOptimize(Start); 58 benchmark::DoNotOptimize(End); 59 while (st.KeepRunning()) { 60 const path P(Start, End); 61 benchmark::DoNotOptimize(P.native().data()); 62 } 63 } 64 template <class GenInputs> 65 void BM_PathConstructInputIter(benchmark::State &st, GenInputs gen) { 66 BM_PathConstructIter<input_iterator>(st, gen); 67 } 68 template <class GenInputs> 69 void BM_PathConstructForwardIter(benchmark::State &st, GenInputs gen) { 70 BM_PathConstructIter<forward_iterator>(st, gen); 71 } 72 BENCHMARK_CAPTURE(BM_PathConstructInputIter, large_string, 73 getRandomStringInputs)->Arg(TestNumInputs); 74 BENCHMARK_CAPTURE(BM_PathConstructForwardIter, large_string, 75 getRandomStringInputs)->Arg(TestNumInputs); 76 77 78 template <class GenInputs> 79 void BM_PathIterateMultipleTimes(benchmark::State &st, GenInputs gen) { 80 using namespace fs; 81 const auto in = gen(st.range(0)); 82 path PP; 83 for (auto& Part : in) 84 PP /= Part; 85 benchmark::DoNotOptimize(PP.native().data()); 86 while (st.KeepRunning()) { 87 for (auto &E : PP) { 88 benchmark::DoNotOptimize(E.native().data()); 89 } 90 benchmark::ClobberMemory(); 91 } 92 } 93 BENCHMARK_CAPTURE(BM_PathIterateMultipleTimes, iterate_elements, 94 getRandomStringInputs)->Arg(TestNumInputs); 95 96 97 template <class GenInputs> 98 void BM_PathIterateOnce(benchmark::State &st, GenInputs gen) { 99 using namespace fs; 100 const auto in = gen(st.range(0)); 101 path PP; 102 for (auto& Part : in) 103 PP /= Part; 104 benchmark::DoNotOptimize(PP.native().data()); 105 while (st.KeepRunning()) { 106 const path P = PP.native(); 107 for (auto &E : P) { 108 benchmark::DoNotOptimize(E.native().data()); 109 } 110 benchmark::ClobberMemory(); 111 } 112 } 113 BENCHMARK_CAPTURE(BM_PathIterateOnce, iterate_elements, 114 getRandomStringInputs)->Arg(TestNumInputs); 115 116 template <class GenInputs> 117 void BM_PathIterateOnceBackwards(benchmark::State &st, GenInputs gen) { 118 using namespace fs; 119 const auto in = gen(st.range(0)); 120 path PP; 121 for (auto& Part : in) 122 PP /= Part; 123 benchmark::DoNotOptimize(PP.native().data()); 124 while (st.KeepRunning()) { 125 const path P = PP.native(); 126 const auto B = P.begin(); 127 auto I = P.end(); 128 while (I != B) { 129 --I; 130 benchmark::DoNotOptimize(*I); 131 } 132 benchmark::DoNotOptimize(*I); 133 } 134 } 135 BENCHMARK_CAPTURE(BM_PathIterateOnceBackwards, iterate_elements, 136 getRandomStringInputs)->Arg(TestNumInputs); 137 138 BENCHMARK_MAIN(); 139