third_party/benchmark/test/map_test.cc
Go to the documentation of this file.
1 #include "benchmark/benchmark.h"
2 
3 #include <cstdlib>
4 #include <map>
5 
6 namespace {
7 
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));
12  }
13  return m;
14 }
15 
16 } // namespace
17 
18 // Basic version.
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));
28  }
29  }
30  state.SetItemsProcessed(state.iterations() * size);
31 }
32 BENCHMARK(BM_MapLookup)->Range(1 << 3, 1 << 12);
33 
34 // Using fixtures.
36  public:
37  void SetUp(const ::benchmark::State& st) BENCHMARK_OVERRIDE {
38  m = ConstructRandomMap(static_cast<int>(st.range(0)));
39  }
40 
41  void TearDown(const ::benchmark::State&) BENCHMARK_OVERRIDE { m.clear(); }
42 
43  std::map<int, int> m;
44 };
45 
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));
51  }
52  }
53  state.SetItemsProcessed(state.iterations() * size);
54 }
55 BENCHMARK_REGISTER_F(MapFixture, Lookup)->Range(1 << 3, 1 << 12);
56 
BENCHMARK_REGISTER_F
BENCHMARK_REGISTER_F(MapFixture, Lookup) -> Range(1<< 3, 1<< 12)
BENCHMARK
BENCHMARK(BM_MapLookup) -> Range(1<< 3, 1<< 12)
MapFixture::m
std::map< int, int > m
Definition: third_party/benchmark/test/map_test.cc:43
BENCHMARK_MAIN
BENCHMARK_MAIN()
MapFixture::SetUp
void SetUp(const ::benchmark::State &st) BENCHMARK_OVERRIDE
Definition: third_party/benchmark/test/map_test.cc:37
benchmark::DoNotOptimize
BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const &value)
Definition: benchmark/include/benchmark/benchmark.h:375
benchmark::Fixture
Definition: benchmark/include/benchmark/benchmark.h:1122
BENCHMARK_DEFINE_F
BENCHMARK_DEFINE_F(MapFixture, Lookup)(benchmark
Definition: third_party/benchmark/test/map_test.cc:46
BENCHMARK_OVERRIDE
#define BENCHMARK_OVERRIDE
Definition: benchmark/include/benchmark/benchmark.h:271
MapFixture::TearDown
void TearDown(const ::benchmark::State &) BENCHMARK_OVERRIDE
Definition: third_party/benchmark/test/map_test.cc:41
MapFixture
Definition: third_party/benchmark/test/map_test.cc:35
gmock_output_test._
_
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
regress.m
m
Definition: regress/regress.py:25
Lookup
static const char * Lookup(std::map< std::string, std::unique_ptr< char[]>> *dummy_lh, const char *key)
Definition: lhash_test.cc:53
BM_MapLookup
static void BM_MapLookup(benchmark::State &state)
Definition: third_party/benchmark/test/map_test.cc:19
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
state
static struct rpc_state state
Definition: bad_server_response_test.cc:87


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:20