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