Last active
December 13, 2022 08:16
-
-
Save koraa/54afbd4504b3451d8d9d to your computer and use it in GitHub Desktop.
C++ Random Engine Benchmark
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
// LICENSE: CC-0 | |
// © 2015 by Karolin Varner | |
// invoke: $ echo boost::random::taus88 boost::random::minstd_rand0 boost::random::minstd_rand boost::random::mt11213b boost::random::mt19937 boost::random::mt19937 std::default_random_engine std::minstd_rand std::minstd_rand0 std::mt19937 std::mt19937_64 | tr ' ' '\n' |while read en; do clang -lboost_random -lboost_system -lstdc++ --std=gnu++11 -DENGINE="$en" -Wall -Wextra bench.cc -o bench; ./bench; done | sort -k2 | column -t | |
#include <boost/uuid/seed_rng.hpp> | |
#include <boost/random/taus88.hpp> | |
#include <boost/random/mersenne_twister.hpp> | |
#include <boost/random/linear_congruential.hpp> | |
#include <boost/random/uniform_int_distribution.hpp> | |
#include <boost/random/uniform_real_distribution.hpp> | |
#include <random> | |
#include <chrono> | |
#include <iostream> | |
using namespace std::chrono; | |
const unsigned int seed = 40641766; | |
#define STR_(a) #a | |
#define STR(a) STR_(a) | |
int main() { | |
ENGINE engine; | |
engine.seed(seed); | |
high_resolution_clock::time_point t1, | |
t0 = high_resolution_clock::now(); | |
for (int i = 0; i < 1000000000; i++) | |
engine(); | |
t1 = high_resolution_clock::now(); | |
duration<double> time_span = | |
duration_cast<duration<double>>(t1 - t0); | |
std::cout << STR(ENGINE) << " " << time_span.count() | |
<< "s" << std::endl; | |
return 0; | |
} |
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
boost::random::mt19937 10.4222s | |
boost::random::mt11213b 10.6539s | |
boost::random::mt19937_64 10.7993s | |
std::mt19937 11.0515s | |
std::mt19937_64 11.0959s | |
std::minstd_rand0 16.2256s | |
std::minstd_rand 16.3615s | |
std::default_random_engine 16.5868s | |
boost::random::taus88 26.7123s | |
boost::random::minstd_rand 38.5173s | |
boost::random::minstd_rand0 38.7661s | |
std::ranlux24 128.103s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment