Skip to content

Instantly share code, notes, and snippets.

@kd9f9
Last active April 18, 2024 04:15
Show Gist options
  • Save kd9f9/c10790978dc48fca35497a307f4939ce to your computer and use it in GitHub Desktop.
Save kd9f9/c10790978dc48fca35497a307f4939ce to your computer and use it in GitHub Desktop.
#include <iostream>
#include <random>
#include <chrono>
#include <iomanip>
template <typename RandomEngineType> void benchmark_random_generator(const std::string &name) {
RandomEngineType generator;
std::uniform_int_distribution<> distribution(0, 1000);
constexpr int nIterations = 300000000;
const auto start = std::chrono::steady_clock::now();
for (int i = 0; i < nIterations; ++i) {
distribution(generator);
}
const auto end = std::chrono::steady_clock::now();
std::cout << name << " " << std::setprecision(3)
<< std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count() / static_cast<double>(nIterations)
<< " ns per random number\n";
};
int main() {
benchmark_random_generator<std::minstd_rand>("minstd_rand");
benchmark_random_generator<std::minstd_rand0>("minstd_rand0");
benchmark_random_generator<std::mt19937>("mt19937");
benchmark_random_generator<std::mt19937_64>("mt19937_64");
benchmark_random_generator<std::ranlux24_base>("ranlux24_base");
benchmark_random_generator<std::ranlux48_base>("ranlux48_base");
benchmark_random_generator<std::ranlux24>("ranlux24");
benchmark_random_generator<std::ranlux48>("ranlux48");
benchmark_random_generator<std::knuth_b>("knuth_b");
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment