Skip to content

Instantly share code, notes, and snippets.

@Terminus-IMRC
Created June 26, 2022 10:27
Show Gist options
  • Save Terminus-IMRC/f5c26788eee91b3a11234b62d089b2dd to your computer and use it in GitHub Desktop.
Save Terminus-IMRC/f5c26788eee91b3a11234b62d089b2dd to your computer and use it in GitHub Desktop.
A benchmark program for C++ random number generators
#include <bit>
#include <random>
#include <benchmark/benchmark.h>
template <class Generator> static void BM_random(benchmark::State &state) {
Generator gen;
for (auto _ : state)
benchmark::DoNotOptimize(gen());
const auto bits{std::bit_width(gen.max() - gen.min())};
state.counters["bits"] = bits;
state.counters["bits_per_second"] =
benchmark::Counter(bits * state.iterations(), benchmark::Counter::kIsRate,
benchmark::Counter::kIs1024);
}
BENCHMARK(BM_random<std::minstd_rand0>);
BENCHMARK(BM_random<std::minstd_rand>);
BENCHMARK(BM_random<std::mt19937>);
BENCHMARK(BM_random<std::mt19937_64>);
BENCHMARK(BM_random<std::ranlux24_base>);
BENCHMARK(BM_random<std::ranlux48_base>);
BENCHMARK(BM_random<std::ranlux24>);
BENCHMARK(BM_random<std::ranlux48>);
BENCHMARK(BM_random<std::knuth_b>);
BENCHMARK(BM_random<std::default_random_engine>);
BENCHMARK(BM_random<std::random_device>);
BENCHMARK_MAIN();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment