Eric Fiselier | c835c12 | 2016-07-02 05:30:54 +0000 | [diff] [blame^] | 1 | #include <unordered_set> |
| 2 | #include <vector> |
| 3 | #include <cstdint> |
| 4 | |
| 5 | #include "benchmark/benchmark_api.h" |
| 6 | |
| 7 | template <class IntT> |
| 8 | std::vector<IntT> getInputs(size_t N) { |
| 9 | std::vector<IntT> inputs; |
| 10 | for (size_t i=0; i < N; ++i) { |
| 11 | inputs.push_back(i); |
| 12 | } |
| 13 | return inputs; |
| 14 | } |
| 15 | |
| 16 | template <class Container, class Inputs> |
| 17 | void BM_SetInsert(benchmark::State& st, Container c, Inputs const& in) { |
| 18 | const auto end = in.end(); |
| 19 | while (st.KeepRunning()) { |
| 20 | c.clear(); |
| 21 | for (auto it = in.begin(); it != end; ++it) { |
| 22 | benchmark::DoNotOptimize(c.insert(*it)); |
| 23 | } |
| 24 | benchmark::DoNotOptimize(c); |
| 25 | } |
| 26 | } |
| 27 | BENCHMARK_CAPTURE(BM_SetInsert, uint32_insert, |
| 28 | std::unordered_set<uint32_t>{}, getInputs<uint32_t>(1024)); |
| 29 | |
| 30 | template <class Container, class Inputs> |
| 31 | void BM_SetFind(benchmark::State& st, Container c, Inputs const& in) { |
| 32 | c.insert(in.begin(), in.end()); |
| 33 | const auto end = in.end(); |
| 34 | while (st.KeepRunning()) { |
| 35 | for (auto it = in.begin(); it != end; ++it) { |
| 36 | benchmark::DoNotOptimize(c.find(*it)); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | BENCHMARK_CAPTURE(BM_SetFind, uint32_lookup, |
| 41 | std::unordered_set<uint32_t>{}, getInputs<uint32_t>(1024)); |
| 42 | |
| 43 | |
| 44 | BENCHMARK_MAIN() |