Last active
February 21, 2018 06:38
-
-
Save parsa/b950aa787a3f48252913728f27991bc9 to your computer and use it in GitHub Desktop.
Crudely Measure Execution Time of PRNGs in <random>
This file contains hidden or 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 <iostream> | |
| #include <random> | |
| #include <chrono> | |
| #include <array> | |
| #include <functional> | |
| #include <algorithm> | |
| struct scoped_timer | |
| { | |
| scoped_timer(std::string name) | |
| : start(std::chrono::high_resolution_clock::now()), test_name(name) {} | |
| ~scoped_timer() | |
| { | |
| auto end = std::chrono::high_resolution_clock::now(); | |
| auto duration = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(end - start); | |
| std::cout << test_name << ": " << duration.count() << "ms\n"; | |
| } | |
| std::chrono::high_resolution_clock::time_point start; | |
| std::string test_name; | |
| }; | |
| const int iterations = 1'000'000; | |
| void test_mt19937() | |
| { | |
| std::array<int, iterations> arr; | |
| std::default_random_engine re; | |
| std::mt19937 g(re()); | |
| std::uniform_int_distribution<> d; | |
| std::generate(arr.begin(), arr.end(), std::bind(d, g)); | |
| } | |
| void test_mt19937_64() | |
| { | |
| std::array<int, iterations> arr; | |
| std::default_random_engine re; | |
| std::mt19937_64 g(re()); | |
| std::uniform_int_distribution<> d; | |
| std::generate(arr.begin(), arr.end(), std::bind(d, g)); | |
| } | |
| void test_ranlux24() | |
| { | |
| std::array<int, iterations> arr; | |
| std::default_random_engine re; | |
| std::ranlux24 g(re()); | |
| std::uniform_int_distribution<> d; | |
| std::generate(arr.begin(), arr.end(), std::bind(d, g)); | |
| } | |
| void test_ranlux48() | |
| { | |
| std::array<int, iterations> arr; | |
| std::default_random_engine re; | |
| std::ranlux48 g(re()); | |
| std::uniform_int_distribution<> d; | |
| std::generate(arr.begin(), arr.end(), std::bind(d, g)); | |
| } | |
| void test_minstd_rand() | |
| { | |
| std::array<int, iterations> arr; | |
| std::default_random_engine re; | |
| std::minstd_rand g(re()); | |
| std::uniform_int_distribution<> d; | |
| std::generate(arr.begin(), arr.end(), std::bind(d, g)); | |
| } | |
| void test_minstd_rand0() | |
| { | |
| std::array<int, iterations> arr; | |
| std::default_random_engine re; | |
| std::minstd_rand0 g(re()); | |
| std::uniform_int_distribution<> d; | |
| std::generate(arr.begin(), arr.end(), std::bind(d, g)); | |
| } | |
| void test_knuth_b() | |
| { | |
| std::array<int, iterations> arr; | |
| std::default_random_engine re; | |
| std::knuth_b g(re()); | |
| std::uniform_int_distribution<> d; | |
| std::generate(arr.begin(), arr.end(), std::bind(d, g)); | |
| } | |
| int main() | |
| { | |
| { | |
| scoped_timer timer("std::mt19937"); | |
| test_mt19937(); | |
| } | |
| { | |
| scoped_timer timer("std::mt19937_64"); | |
| test_mt19937_64(); | |
| } | |
| { | |
| scoped_timer timer("std::ranlux24"); | |
| test_ranlux24(); | |
| } | |
| { | |
| scoped_timer timer("std::ranlux48"); | |
| test_ranlux48(); | |
| } | |
| { | |
| scoped_timer timer("std::minstd_rand"); | |
| test_minstd_rand(); | |
| } | |
| { | |
| scoped_timer timer("std::knuth_b"); | |
| test_knuth_b(); | |
| } | |
| } | |
| /* | |
| Output: | |
| std::mt19937: 31.5352ms | |
| std::mt19937_64: 22.8085ms | |
| std::ranlux24: 216.86ms | |
| std::ranlux48: 332.197ms | |
| std::minstd_rand: 49.1883ms | |
| std::knuth_b: 57.1808ms | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment