1 #include "benchmark/benchmark.h"
8 std::map
<int, int> ConstructRandomMap(int size
) {
10 for (int i
= 0; i
< size
; ++i
) {
11 m
.insert(std::make_pair(std::rand() % size
, std::rand() % size
));
19 static void BM_MapLookup(benchmark::State
& state
) {
20 const int size
= static_cast<int>(state
.range(0));
22 for (auto _
: state
) {
24 m
= ConstructRandomMap(size
);
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);
35 class MapFixture
: public ::benchmark::Fixture
{
37 void SetUp(const ::benchmark::State
& st
) {
38 m
= ConstructRandomMap(static_cast<int>(st
.range(0)));
41 void TearDown(const ::benchmark::State
&) { m
.clear(); }
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);