[llvm-readobj] - Implement LLVM-style dumping for .stack_sizes sections.
[llvm-complete.git] / utils / benchmark / test / map_test.cc
blobdbf7982a3686e0aba0380134a6de236852f66ca4
1 #include "benchmark/benchmark.h"
3 #include <cstdlib>
4 #include <map>
6 namespace {
8 std::map<int, int> ConstructRandomMap(int size) {
9 std::map<int, int> m;
10 for (int i = 0; i < size; ++i) {
11 m.insert(std::make_pair(std::rand() % size, std::rand() % size));
13 return m;
16 } // namespace
18 // Basic version.
19 static void BM_MapLookup(benchmark::State& state) {
20 const int size = static_cast<int>(state.range(0));
21 std::map<int, int> m;
22 for (auto _ : state) {
23 state.PauseTiming();
24 m = ConstructRandomMap(size);
25 state.ResumeTiming();
26 for (int i = 0; i < size; ++i) {
27 benchmark::DoNotOptimize(m.find(std::rand() % size));
30 state.SetItemsProcessed(state.iterations() * size);
32 BENCHMARK(BM_MapLookup)->Range(1 << 3, 1 << 12);
34 // Using fixtures.
35 class MapFixture : public ::benchmark::Fixture {
36 public:
37 void SetUp(const ::benchmark::State& st) {
38 m = ConstructRandomMap(static_cast<int>(st.range(0)));
41 void TearDown(const ::benchmark::State&) { m.clear(); }
43 std::map<int, int> m;
46 BENCHMARK_DEFINE_F(MapFixture, Lookup)(benchmark::State& state) {
47 const int size = static_cast<int>(state.range(0));
48 for (auto _ : state) {
49 for (int i = 0; i < size; ++i) {
50 benchmark::DoNotOptimize(m.find(std::rand() % size));
53 state.SetItemsProcessed(state.iterations() * size);
55 BENCHMARK_REGISTER_F(MapFixture, Lookup)->Range(1 << 3, 1 << 12);
57 BENCHMARK_MAIN();