mirror of
https://github.com/google/benchmark.git
synced 2024-12-01 07:16:07 +00:00
fa341e51cb
* Fix BM_SetInsert example Move declaration of `std::set<int> data` outside the timing loop, so that the destructor is not timed. * Speed up BM_SetInsert test Since the time taken to ConstructRandomSet() is so large compared to the time to insert one element, but only the latter is used to determine number of iterations, this benchmark now takes an extremely long time to run in benchmark_test. Speed it up two ways: - Increase the Ranges() parameters - Cache ConstructRandomSet() result (it's not random anyway), and do only O(N) copy every iteration * Fix same issue in BM_MapLookup test * Make BM_SetInsert test consistent with README - Use the same Ranges everywhere, but increase the 2nd range - Change order of Args() calls in README to more closely match the result of Ranges - Don't cache ConstructRandomSet, since it doesn't make sense in README - Get a smaller optimization inside it, by givint a hint to insert()
58 lines
1.3 KiB
C++
58 lines
1.3 KiB
C++
#include "benchmark/benchmark.h"
|
|
|
|
#include <cstdlib>
|
|
#include <map>
|
|
|
|
namespace {
|
|
|
|
std::map<int, int> ConstructRandomMap(int size) {
|
|
std::map<int, int> m;
|
|
for (int i = 0; i < size; ++i) {
|
|
m.insert(std::make_pair(rand() % size, rand() % size));
|
|
}
|
|
return m;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
// Basic version.
|
|
static void BM_MapLookup(benchmark::State& state) {
|
|
const int size = state.range(0);
|
|
std::map<int, int> m;
|
|
for (auto _ : state) {
|
|
state.PauseTiming();
|
|
m = ConstructRandomMap(size);
|
|
state.ResumeTiming();
|
|
for (int i = 0; i < size; ++i) {
|
|
benchmark::DoNotOptimize(m.find(rand() % size));
|
|
}
|
|
}
|
|
state.SetItemsProcessed(state.iterations() * size);
|
|
}
|
|
BENCHMARK(BM_MapLookup)->Range(1 << 3, 1 << 12);
|
|
|
|
// Using fixtures.
|
|
class MapFixture : public ::benchmark::Fixture {
|
|
public:
|
|
void SetUp(const ::benchmark::State& st) {
|
|
m = ConstructRandomMap(st.range(0));
|
|
}
|
|
|
|
void TearDown(const ::benchmark::State&) { m.clear(); }
|
|
|
|
std::map<int, int> m;
|
|
};
|
|
|
|
BENCHMARK_DEFINE_F(MapFixture, Lookup)(benchmark::State& state) {
|
|
const int size = state.range(0);
|
|
for (auto _ : state) {
|
|
for (int i = 0; i < size; ++i) {
|
|
benchmark::DoNotOptimize(m.find(rand() % size));
|
|
}
|
|
}
|
|
state.SetItemsProcessed(state.iterations() * size);
|
|
}
|
|
BENCHMARK_REGISTER_F(MapFixture, Lookup)->Range(1 << 3, 1 << 12);
|
|
|
|
BENCHMARK_MAIN()
|