1 #ifndef BENCHMARK_CONTAINER_BENCHMARKS_HPP 2 #define BENCHMARK_CONTAINER_BENCHMARKS_HPP 3 4 #include <cassert> 5 6 #include "benchmark/benchmark.h" 7 8 namespace ContainerBenchmarks { 9 10 11 template <class Container, class GenInputs> 12 void BM_ConstructIterIter(benchmark::State& st, Container, GenInputs gen) { 13 auto in = gen(st.range(0)); 14 const auto begin = in.begin(); 15 const auto end = in.end(); 16 benchmark::DoNotOptimize(&in); 17 while (st.KeepRunning()) { 18 Container c(begin, end); 19 benchmark::DoNotOptimize(c.data()); 20 } 21 } 22 23 template <class Container, class GenInputs> 24 void BM_InsertValue(benchmark::State& st, Container c, GenInputs gen) { 25 auto in = gen(st.range(0)); 26 const auto end = in.end(); 27 while (st.KeepRunning()) { 28 c.clear(); 29 for (auto it = in.begin(); it != end; ++it) { 30 benchmark::DoNotOptimize(&(*c.insert(*it).first)); 31 } 32 benchmark::ClobberMemory(); 33 } 34 } 35 36 template <class Container, class GenInputs> 37 void BM_InsertValueRehash(benchmark::State& st, Container c, GenInputs gen) { 38 auto in = gen(st.range(0)); 39 const auto end = in.end(); 40 while (st.KeepRunning()) { 41 c.clear(); 42 c.rehash(16); 43 for (auto it = in.begin(); it != end; ++it) { 44 benchmark::DoNotOptimize(&(*c.insert(*it).first)); 45 } 46 benchmark::ClobberMemory(); 47 } 48 } 49 50 51 template <class Container, class GenInputs> 52 void BM_InsertDuplicate(benchmark::State& st, Container c, GenInputs gen) { 53 auto in = gen(st.range(0)); 54 const auto end = in.end(); 55 c.insert(in.begin(), in.end()); 56 benchmark::DoNotOptimize(&c); 57 benchmark::DoNotOptimize(&in); 58 while (st.KeepRunning()) { 59 for (auto it = in.begin(); it != end; ++it) { 60 benchmark::DoNotOptimize(&(*c.insert(*it).first)); 61 } 62 benchmark::ClobberMemory(); 63 } 64 } 65 66 67 template <class Container, class GenInputs> 68 void BM_EmplaceDuplicate(benchmark::State& st, Container c, GenInputs gen) { 69 auto in = gen(st.range(0)); 70 const auto end = in.end(); 71 c.insert(in.begin(), in.end()); 72 benchmark::DoNotOptimize(&c); 73 benchmark::DoNotOptimize(&in); 74 while (st.KeepRunning()) { 75 for (auto it = in.begin(); it != end; ++it) { 76 benchmark::DoNotOptimize(&(*c.emplace(*it).first)); 77 } 78 benchmark::ClobberMemory(); 79 } 80 } 81 82 template <class Container, class GenInputs> 83 static void BM_Find(benchmark::State& st, Container c, GenInputs gen) { 84 auto in = gen(st.range(0)); 85 c.insert(in.begin(), in.end()); 86 benchmark::DoNotOptimize(&(*c.begin())); 87 const auto end = in.data() + in.size(); 88 while (st.KeepRunning()) { 89 for (auto it = in.data(); it != end; ++it) { 90 benchmark::DoNotOptimize(&(*c.find(*it))); 91 } 92 benchmark::ClobberMemory(); 93 } 94 } 95 96 template <class Container, class GenInputs> 97 static void BM_FindRehash(benchmark::State& st, Container c, GenInputs gen) { 98 c.rehash(8); 99 auto in = gen(st.range(0)); 100 c.insert(in.begin(), in.end()); 101 benchmark::DoNotOptimize(&(*c.begin())); 102 const auto end = in.data() + in.size(); 103 while (st.KeepRunning()) { 104 for (auto it = in.data(); it != end; ++it) { 105 benchmark::DoNotOptimize(&(*c.find(*it))); 106 } 107 benchmark::ClobberMemory(); 108 } 109 } 110 111 } // end namespace ContainerBenchmarks 112 113 #endif // BENCHMARK_CONTAINER_BENCHMARKS_HPP 114