xref: /aosp_15_r20/external/libcxx/utils/google-benchmark/test/map_test.cc (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
1*58b9f456SAndroid Build Coastguard Worker #include "benchmark/benchmark.h"
2*58b9f456SAndroid Build Coastguard Worker 
3*58b9f456SAndroid Build Coastguard Worker #include <cstdlib>
4*58b9f456SAndroid Build Coastguard Worker #include <map>
5*58b9f456SAndroid Build Coastguard Worker 
6*58b9f456SAndroid Build Coastguard Worker namespace {
7*58b9f456SAndroid Build Coastguard Worker 
ConstructRandomMap(int size)8*58b9f456SAndroid Build Coastguard Worker std::map<int, int> ConstructRandomMap(int size) {
9*58b9f456SAndroid Build Coastguard Worker   std::map<int, int> m;
10*58b9f456SAndroid Build Coastguard Worker   for (int i = 0; i < size; ++i) {
11*58b9f456SAndroid Build Coastguard Worker     m.insert(std::make_pair(std::rand() % size, std::rand() % size));
12*58b9f456SAndroid Build Coastguard Worker   }
13*58b9f456SAndroid Build Coastguard Worker   return m;
14*58b9f456SAndroid Build Coastguard Worker }
15*58b9f456SAndroid Build Coastguard Worker 
16*58b9f456SAndroid Build Coastguard Worker }  // namespace
17*58b9f456SAndroid Build Coastguard Worker 
18*58b9f456SAndroid Build Coastguard Worker // Basic version.
BM_MapLookup(benchmark::State & state)19*58b9f456SAndroid Build Coastguard Worker static void BM_MapLookup(benchmark::State& state) {
20*58b9f456SAndroid Build Coastguard Worker   const int size = static_cast<int>(state.range(0));
21*58b9f456SAndroid Build Coastguard Worker   std::map<int, int> m;
22*58b9f456SAndroid Build Coastguard Worker   for (auto _ : state) {
23*58b9f456SAndroid Build Coastguard Worker     state.PauseTiming();
24*58b9f456SAndroid Build Coastguard Worker     m = ConstructRandomMap(size);
25*58b9f456SAndroid Build Coastguard Worker     state.ResumeTiming();
26*58b9f456SAndroid Build Coastguard Worker     for (int i = 0; i < size; ++i) {
27*58b9f456SAndroid Build Coastguard Worker       benchmark::DoNotOptimize(m.find(std::rand() % size));
28*58b9f456SAndroid Build Coastguard Worker     }
29*58b9f456SAndroid Build Coastguard Worker   }
30*58b9f456SAndroid Build Coastguard Worker   state.SetItemsProcessed(state.iterations() * size);
31*58b9f456SAndroid Build Coastguard Worker }
32*58b9f456SAndroid Build Coastguard Worker BENCHMARK(BM_MapLookup)->Range(1 << 3, 1 << 12);
33*58b9f456SAndroid Build Coastguard Worker 
34*58b9f456SAndroid Build Coastguard Worker // Using fixtures.
35*58b9f456SAndroid Build Coastguard Worker class MapFixture : public ::benchmark::Fixture {
36*58b9f456SAndroid Build Coastguard Worker  public:
SetUp(const::benchmark::State & st)37*58b9f456SAndroid Build Coastguard Worker   void SetUp(const ::benchmark::State& st) {
38*58b9f456SAndroid Build Coastguard Worker     m = ConstructRandomMap(static_cast<int>(st.range(0)));
39*58b9f456SAndroid Build Coastguard Worker   }
40*58b9f456SAndroid Build Coastguard Worker 
TearDown(const::benchmark::State &)41*58b9f456SAndroid Build Coastguard Worker   void TearDown(const ::benchmark::State&) { m.clear(); }
42*58b9f456SAndroid Build Coastguard Worker 
43*58b9f456SAndroid Build Coastguard Worker   std::map<int, int> m;
44*58b9f456SAndroid Build Coastguard Worker };
45*58b9f456SAndroid Build Coastguard Worker 
BENCHMARK_DEFINE_F(MapFixture,Lookup)46*58b9f456SAndroid Build Coastguard Worker BENCHMARK_DEFINE_F(MapFixture, Lookup)(benchmark::State& state) {
47*58b9f456SAndroid Build Coastguard Worker   const int size = static_cast<int>(state.range(0));
48*58b9f456SAndroid Build Coastguard Worker   for (auto _ : state) {
49*58b9f456SAndroid Build Coastguard Worker     for (int i = 0; i < size; ++i) {
50*58b9f456SAndroid Build Coastguard Worker       benchmark::DoNotOptimize(m.find(std::rand() % size));
51*58b9f456SAndroid Build Coastguard Worker     }
52*58b9f456SAndroid Build Coastguard Worker   }
53*58b9f456SAndroid Build Coastguard Worker   state.SetItemsProcessed(state.iterations() * size);
54*58b9f456SAndroid Build Coastguard Worker }
55*58b9f456SAndroid Build Coastguard Worker BENCHMARK_REGISTER_F(MapFixture, Lookup)->Range(1 << 3, 1 << 12);
56*58b9f456SAndroid Build Coastguard Worker 
57*58b9f456SAndroid Build Coastguard Worker BENCHMARK_MAIN();
58