-
-
Save StephanDollberg/62097375c863687490eb027b5941f87c to your computer and use it in GitHub Desktop.
Prefault comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <benchmark/benchmark.h> | |
#include <absl/container/flat_hash_map.h> | |
#define BENCH_SIZES ->Arg(1000)->Arg(1000000)->Arg(10000000) | |
template <typename Map> | |
void fill_map(Map& map, const std::uint64_t size) { | |
for (std::uint64_t i = 0 ; i < size; ++i) { | |
map.try_emplace(i, i*2); | |
} | |
} | |
template <typename Map> | |
std::uint64_t sum_map(Map& map) { | |
std::uint64_t sum = 0; | |
for (auto&& [k,v]: map) { | |
sum += k + 2 * v; | |
} | |
return sum; | |
} | |
static void with_reserve(benchmark::State& state) { | |
const std::size_t test_size = state.range(0); | |
for (auto __attribute__((unused)) _ : state) { | |
state.PauseTiming(); | |
absl::flat_hash_map<std::uint64_t, std::uint64_t> map; | |
map.reserve(test_size); | |
state.ResumeTiming(); | |
fill_map(map, test_size); | |
benchmark::DoNotOptimize(sum_map(map)); | |
} | |
} | |
BENCHMARK(with_reserve) | |
BENCH_SIZES; | |
static void no_reserve(benchmark::State& state) { | |
const std::size_t test_size = state.range(0); | |
for (auto __attribute__((unused)) _ : state) { | |
state.PauseTiming(); | |
absl::flat_hash_map<std::uint64_t, std::uint64_t> map; | |
state.ResumeTiming(); | |
fill_map(map, test_size); | |
benchmark::DoNotOptimize(sum_map(map)); | |
} | |
} | |
BENCHMARK(no_reserve) | |
BENCH_SIZES; | |
int main(int argc, char** argv) { | |
benchmark::Initialize(&argc, argv); | |
benchmark::RunSpecifiedBenchmarks(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment