Skip to content

C++ Benchmarks and comparisons of C++ pseudo random number generators (PRNG)

License

Notifications You must be signed in to change notification settings

zeFresk/cpp-random-benchmarks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Speed comparison & benchmark of differents C++ random number generators (PRNG)

This repository provides an easy way to compare some common pseudo random number generators (PRNGS) used in C++. It also provides my results running the benchmarks for those wanting a quick answer. To reproduce my results and test for yourself, see section Test for yourself. As of now, the tested PRNG are: Mersenne twister, Linear Congruential Engine, xoshiro / xorshift*, PCG, rand(), Substract with carry and some other more exotic engines.

⚠️
Most of these algorithms should not be used in a context where security is required.

Results

All these results were compiled over 100 hundred runs (--benchmark_repetitions=100) using an Intel i5-3570 @ 3.40GHz…​.. The binary was compiled with clang using -O3 -DNDEBUG, without any architecture optimization. The code is written to avoid any clever trick from the compiler that could bias the results.

Table 1. Table of results

PRNG (link)

Mean time per generation (user)

Mean generation per second

Remarks

xoshiro128++

1.06ns ± 0.002 ns

946.757M/s ± 0.719M/s

xoshiro256++

1.06ns ± 0.002 ns

946.704M/s ± 0.581M/s

Fastest for 64 bits, good quality, 400 lines

pcg_cpp::pcg32

1.09ns ± 0.010ns

916.834M/s ± 8.424M/s

pcg_cpp::pcg32_fast

0.933ns ± 0.006ns

1071.970M/s ± 6.220M/s

Fastest for 32 bits

pcg_cpp::pcg64_oneseq

1.09ns ± 0.010ns

916.063M/s ± 8.155M/s

pcg_cpp::pcg64

1.98ns ± 0.013ns

504.325M/s ± 3.121M/s

pcg_cpp::pcg64_fast

1.31ns ± 0.012ns

763.500M/s ± 6.629M/s

pcg_cpp::pcg64_oneseq

1.98ns ± 0.008ns

504.750M/s ± 2.064M/s

wjakob pcg32

1.06ns ± 0.012ns

940.393M/s ± 10.598M/s

Not that much faster than pcg-cpp

wjakob pcg32_8

8.48ns ± 0.112ns

944.200M/s ± 12.215M/s

Fastest if your CPU have AVX, mine does not

burtle burtle C random

2.79ns ± 0.023ns

358.876M/s ± 2.627M/s

20 lines implementation

C rand()

15.4ns ± 0.123ns

65.005M/s ± 0.509M/s

Slow and poor quality, avoid

C++ STL std::mt19937 (MT)

3.70ns ± 0.048ns

270.652M ± 3.418M/s

Best STL generator for portability

C++ STL std::mt19937_64 (MT)

3.40ns ± 0.028ns

294.261M ± 2.352M/s

Best STL generator for x86_64

C++ STL std::minstd_rand (LCG)

3.72ns ± 0.038ns

269.078M ± 2.705M/s

Worse quality than mt19937 and slower

C++ STL std::minstd_rand0 (LCG)

3.72ns ± 0.033ns

268.768M ± 2.336M/s

Worse quality than mt19937 and slower

C++ STL std::ranlux24_base (SWC)

3.27ns ± 0.022ns

306.088M ± 2.013M/s

Poor quality

C++ STL std::ranlux48_base (SWC)

3.28ns ± 0.038ns

305.131M ± 3.484/s

Poor quality

C++ STL std::ranlux24 (DBE)

25.8ns ± 0.243ns

38.809M ± 0.356M/s

Awfully slow

C++ STL std::ranlux48 (DBE)

98.3ns ± 0.926ns

10.18M ± 0.093M/s

Awfully slow

C++ STL std::knuth_b (SOE)

14.9ns ± 0.153ns

66.990M ± 0.660M/s

Better quality than minstd_rand but as slow as rand

Plot of the measured speed of PRNGs

Conclusion

std::mt19937 and std::mt19937_64 are the best standard C pseudo random number generators in the STL. However, https://www.pcg-random.org/[PCG] is superior in all aspects **for non-cryptographic** uses. https://prng.di.unimi.it/[xoshiro256] is another very good choice for generating 64 bits random numbers, but it does not support multiple streams like PCG.

rand() need to be avoided at all costs, it is very slow and its quality is bad.

Test for yourself

CMake will automatically download all dependencies from other repositories. You just need to have CMake and C++ compiler installed.

  1. Clone this repository using git

    git clone https://github.com/zeFresk/cpp-random-benchmarks.git
  2. Compile using CMake :

    cmake .
    cmake --build .
  3. Run and publish your results

    ./random_benchmarks

About

C++ Benchmarks and comparisons of C++ pseudo random number generators (PRNG)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published