Last active
March 25, 2021 11:15
-
-
Save odarbelaeze/e79e6d30da2a7f7b984f to your computer and use it in GitHub Desktop.
Random number generator benchmarks
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 <cstdlib> | |
#include <iostream> | |
#include <chrono> | |
#include <random> | |
#include<gsl/gsl_rng.h> | |
class Timer | |
{ | |
private: | |
std::chrono::high_resolution_clock::time_point start_time; | |
std::chrono::high_resolution_clock::time_point stop_time; | |
public: | |
void start() | |
{ | |
start_time = std::chrono::high_resolution_clock::now(); | |
} | |
void stop() | |
{ | |
stop_time = std::chrono::high_resolution_clock::now(); | |
} | |
double measure() | |
{ | |
using namespace std::chrono; | |
return duration_cast<nanoseconds> | |
(stop_time - start_time).count() / 1.0; | |
} | |
}; | |
template<typename T> | |
class Random | |
{ | |
private: | |
T generator; | |
public: | |
Random() | |
: generator | |
(std::chrono::high_resolution_clock::now().time_since_epoch().count()) | |
{ | |
} | |
int generate_integer(int begin, int end) | |
{ | |
return std::uniform_int_distribution<int>(begin, end - 1)(generator); | |
} | |
}; | |
int main() | |
{ | |
constexpr int n = 300000000; | |
Random<std::minstd_rand> mr; | |
Random<std::mt19937> mt; | |
Random<std::mt19937_64> mt64; | |
gsl_rng * gslmt = gsl_rng_alloc (gsl_rng_mt19937); | |
Timer t; | |
for (int j = 0; j < 3; ++j) | |
{ | |
t.start(); | |
for (int i = 0; i < n; ++i) | |
{ | |
/* static_cast<volatile void> */(std::rand() % 10); | |
} | |
t.stop(); | |
std::cout << "rand " << t.measure() /n << " ns per rand" << std::endl; | |
t.start(); | |
for (int i = 0; i < n; ++i) | |
{ | |
/* static_cast<volatile void> */(mr.generate_integer(0, 10)); | |
} | |
t.stop(); | |
std::cout << "minstd " << t.measure() /n << " ns per rand" << std::endl; | |
t.start(); | |
for (int i = 0; i < n; ++i) | |
{ | |
/* static_cast<volatile void> */(mt.generate_integer(0, 10)); | |
} | |
t.stop(); | |
std::cout << "mersenne " << t.measure() /n << " ns per rand" << std::endl; | |
t.start(); | |
for (int i = 0; i < n; ++i) | |
{ | |
/* static_cast<volatile void> */(mt64.generate_integer(0, 10)); | |
} | |
t.stop(); | |
std::cout << "mersenne 64 " << t.measure() /n << " ns per rand" << std::endl; | |
t.start(); | |
for (int i = 0; i < n; ++i) | |
{ | |
/* static_cast<volatile void> */(gsl_rng_get(gslmt)); | |
} | |
t.stop(); | |
std::cout << "gsl mersenne " << t.measure() /n << " ns per rand" << std::endl; | |
} | |
gsl_rng_free(gslmt); | |
} |
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
pcm@PCMC01W01:~ | |
▶ ./rand | |
rand 16.3945 ns per rand | |
minstd 20.1129 ns per rand | |
mersenne 18.9779 ns per rand | |
mersenne 64 4.77588 ns per rand | |
gsl mersenne 6.10239 ns per rand | |
rand 16.1322 ns per rand | |
minstd 20.1151 ns per rand | |
mersenne 18.9815 ns per rand | |
mersenne 64 4.77733 ns per rand | |
gsl mersenne 6.10307 ns per rand | |
rand 16.1384 ns per rand | |
minstd 20.1181 ns per rand | |
mersenne 18.9783 ns per rand | |
mersenne 64 4.77653 ns per rand | |
gsl mersenne 6.10385 ns per rand | |
pcm@PCMC01W01:~ | |
▶ gcc --version | |
gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4 | |
Copyright (C) 2013 Free Software Foundation, Inc. | |
This is free software; see the source for copying conditions. There is NO | |
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
▶ ./rand | |
rand 19.3163 ns per rand | |
minstd 21.7224 ns per rand | |
mersenne 25.5529 ns per rand | |
mersenne 64 5.55641 ns per rand | |
gsl mersenne 7.55933 ns per rand | |
rand 18.3189 ns per rand | |
minstd 21.7219 ns per rand | |
mersenne 25.5505 ns per rand | |
mersenne 64 5.55641 ns per rand | |
gsl mersenne 7.56132 ns per rand | |
rand 18.3168 ns per rand | |
minstd 21.7207 ns per rand | |
mersenne 25.55 ns per rand | |
mersenne 64 5.55821 ns per rand | |
gsl mersenne 7.5594 ns per rand | |
pcm@PCMWS1:~ | |
▶ gcc --version | |
gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4 | |
Copyright (C) 2013 Free Software Foundation, Inc. | |
This is free software; see the source for copying conditions. There is NO | |
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment