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