Generate length: 8
Time difference = 310[ns] std::default_random_engine(std::time(nullptr))
Time difference = 355[ns] std::minstd_rand(std::time(nullptr))
Time difference = 471[ns] std::minstd_rand0(std::time(nullptr))
Time difference = 1178[ns] std::ranlux24(std::time(nullptr))
Time difference = 1186[ns] std::ranlux48(std::time(nullptr))
Time difference = 1226[ns] std::ranlux48_base(std::time(nullptr))
Time difference = 1431[ns] std::ranlux24_base(std::time(nullptr))
Time difference = 4469[ns] std::minstd_rand(std::random_device())
Time difference = 4498[ns] std::default_random_engine(std::random_device())
Time difference = 4572[ns] std::srand(std::time(nullptr))
Time difference = 4921[ns] std::minstd_rand0(std::random_device())
Time difference = 5401[ns] std::ranlux48_base(std::random_device())
Time difference = 5442[ns] std::random_device()
Time difference = 5534[ns] std::ranlux24_base(std::random_device())
Time difference = 5904[ns] std::knuth_b(std::time(nullptr))
Time difference = 5922[ns] std::ranlux48(std::random_device())
Time difference = 6447[ns] std::ranlux24(std::random_device())
Time difference = 9585[ns] std::mt19937_64(std::time(nullptr))
Time difference = 10216[ns] std::knuth_b(std::random_device())
Time difference = 13339[ns] std::mt19937_64(std::random_device())
Time difference = 15963[ns] std::srand(std::random_device())
Time difference = 19858[ns] std::mt19937(std::time(nullptr))
Time difference = 25225[ns] std::mt19937(std::random_device())
Time difference = 109233[ns] std::rand()
Generate length: 16777216
Time difference = 501085680[ns] std::mt19937_64(std::random_device())
Time difference = 550391903[ns] std::mt19937(std::random_device())
Time difference = 565586971[ns] std::mt19937_64(std::time(nullptr))
Time difference = 611410753[ns] std::mt19937(std::time(nullptr))
Time difference = 679359820[ns] std::ranlux24_base(std::time(nullptr))
Time difference = 691367672[ns] std::ranlux24_base(std::random_device())
Time difference = 739911368[ns] std::ranlux48_base(std::random_device())
Time difference = 765268101[ns] std::ranlux48_base(std::time(nullptr))
Time difference = 769522478[ns] std::rand()
Time difference = 811722105[ns] std::minstd_rand0(std::time(nullptr))
Time difference = 926755976[ns] std::srand(std::time(nullptr))
Time difference = 946356341[ns] std::minstd_rand0(std::random_device())
Time difference = 993438372[ns] std::srand(std::random_device())
Time difference = 1821175807[ns] std::minstd_rand(std::random_device())
Time difference = 1888841659[ns] std::default_random_engine(std::time(nullptr))
Time difference = 1934448406[ns] std::default_random_engine(std::random_device())
Time difference = 2309083774[ns] std::knuth_b(std::time(nullptr))
Time difference = 2616665864[ns] std::minstd_rand(std::time(nullptr))
Time difference = 3447330778[ns] std::knuth_b(std::random_device())
Time difference = 6943738731[ns] std::ranlux24(std::random_device())
Time difference = 7042813557[ns] std::ranlux24(std::time(nullptr))
Time difference = 9691396650[ns] std::random_device()
Time difference = 27929253110[ns] std::ranlux48(std::time(nullptr))
Time difference = 32293483788[ns] std::ranlux48(std::random_device())
Last active
June 5, 2021 16:04
-
-
Save playday3008/5e178922f629e2eb6fe8a00eee30b9bb to your computer and use it in GitHub Desktop.
Speedtest all (I hope) PRNG (Pseudorandom number generator) on C++
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 <algorithm> | |
| #include <chrono> | |
| #include <iostream> | |
| #include <random> | |
| #include <sstream> | |
| auto rand_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| while (length != 0) { | |
| std::rand(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::rand()" << std::endl; | |
| return ret.str(); | |
| } | |
| auto srand_time_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::srand(std::time(nullptr)); | |
| while (length != 0) { | |
| std::rand(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::srand(std::time(nullptr))" << std::endl; | |
| return ret.str(); | |
| } | |
| auto srand_rd_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::random_device rd; | |
| std::srand(rd()); | |
| while (length != 0) { | |
| std::rand(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::srand(std::random_device())" << std::endl; | |
| return ret.str(); | |
| } | |
| auto minstd_rand0_time_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::minstd_rand0 gen(std::time(nullptr)); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::minstd_rand0(std::time(nullptr))" << std::endl; | |
| return ret.str(); | |
| } | |
| auto minstd_rand0_rd_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::random_device rd; | |
| std::minstd_rand0 gen(rd()); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::minstd_rand0(std::random_device())" << std::endl; | |
| return ret.str(); | |
| } | |
| auto minstd_rand_time_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::minstd_rand gen(std::time(nullptr)); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::minstd_rand(std::time(nullptr))" << std::endl; | |
| return ret.str(); | |
| } | |
| auto minstd_rand_rd_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::random_device rd; | |
| std::minstd_rand gen(rd()); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::minstd_rand(std::random_device())" << std::endl; | |
| return ret.str(); | |
| } | |
| auto mt19937_time_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::mt19937 gen(std::time(nullptr)); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::mt19937(std::time(nullptr))" << std::endl; | |
| return ret.str(); | |
| } | |
| auto mt19937_rd_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::random_device rd; | |
| std::mt19937 gen(rd()); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::mt19937(std::random_device())" << std::endl; | |
| return ret.str(); | |
| } | |
| auto mt19937_64_time_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::mt19937_64 gen(std::time(nullptr)); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::mt19937_64(std::time(nullptr))" << std::endl; | |
| return ret.str(); | |
| } | |
| auto mt19937_64_rd_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::random_device rd; | |
| std::mt19937_64 gen(rd()); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::mt19937_64(std::random_device())" << std::endl; | |
| return ret.str(); | |
| } | |
| auto ranlux24_base_time_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::ranlux24_base gen(std::time(nullptr)); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::ranlux24_base(std::time(nullptr))" << std::endl; | |
| return ret.str(); | |
| } | |
| auto ranlux24_base_rd_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::random_device rd; | |
| std::ranlux24_base gen(rd()); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::ranlux24_base(std::random_device())" << std::endl; | |
| return ret.str(); | |
| } | |
| auto ranlux48_base_time_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::ranlux48_base gen(std::time(nullptr)); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::ranlux48_base(std::time(nullptr))" << std::endl; | |
| return ret.str(); | |
| } | |
| auto ranlux48_base_rd_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::random_device rd; | |
| std::ranlux48_base gen(rd()); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::ranlux48_base(std::random_device())" << std::endl; | |
| return ret.str(); | |
| } | |
| auto ranlux24_time_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::ranlux24 gen(std::time(nullptr)); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::ranlux24(std::time(nullptr))" << std::endl; | |
| return ret.str(); | |
| } | |
| auto ranlux24_rd_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::random_device rd; | |
| std::ranlux24 gen(rd()); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::ranlux24(std::random_device())" << std::endl; | |
| return ret.str(); | |
| } | |
| auto ranlux48_time_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::ranlux48 gen(std::time(nullptr)); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() << "[ns] std::ranlux48(std::time(nullptr))" << std::endl; | |
| return ret.str(); | |
| } | |
| auto ranlux48_rd_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::random_device rd; | |
| std::ranlux48 gen(rd()); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::ranlux48(std::random_device())" << std::endl; | |
| return ret.str(); | |
| } | |
| auto knuth_b_time_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::knuth_b gen(std::time(nullptr)); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::knuth_b(std::time(nullptr))" << std::endl; | |
| return ret.str(); | |
| } | |
| auto knuth_b_rd_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::random_device rd; | |
| std::knuth_b gen(rd()); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::knuth_b(std::random_device())" << std::endl; | |
| return ret.str(); | |
| } | |
| auto default_random_engine_time_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::default_random_engine gen(std::time(nullptr)); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::default_random_engine(std::time(nullptr))" << std::endl; | |
| return ret.str(); | |
| } | |
| auto default_random_engine_rd_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::random_device rd; | |
| std::default_random_engine gen(rd()); | |
| while (length != 0) { | |
| gen(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::default_random_engine(std::random_device())" << std::endl; | |
| return ret.str(); | |
| } | |
| auto random_device_st(unsigned long long length) | |
| { | |
| std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now(); | |
| std::random_device rd; | |
| while (length != 0) { | |
| rd(); | |
| length--; | |
| }; | |
| std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); | |
| std::stringstream ret; | |
| ret << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns] std::random_device()" << std::endl; | |
| return ret.str(); | |
| } | |
| void show_diff(unsigned long long length) { | |
| std::cout << "Generate length: " << std::fixed << length << std::endl; | |
| std::vector<std::string> output; | |
| output.push_back(rand_st(length)); | |
| output.push_back(srand_time_st(length)); | |
| output.push_back(srand_rd_st(length)); | |
| output.push_back(minstd_rand0_time_st(length)); | |
| output.push_back(minstd_rand0_rd_st(length)); | |
| output.push_back(minstd_rand_time_st(length)); | |
| output.push_back(minstd_rand_rd_st(length)); | |
| output.push_back(mt19937_time_st(length)); | |
| output.push_back(mt19937_rd_st(length)); | |
| output.push_back(mt19937_64_time_st(length)); | |
| output.push_back(mt19937_64_rd_st(length)); | |
| output.push_back(ranlux24_base_time_st(length)); | |
| output.push_back(ranlux24_base_rd_st(length)); | |
| output.push_back(ranlux48_base_time_st(length)); | |
| output.push_back(ranlux48_base_rd_st(length)); | |
| output.push_back(ranlux24_time_st(length)); | |
| output.push_back(ranlux24_rd_st(length)); | |
| output.push_back(ranlux48_time_st(length)); | |
| output.push_back(ranlux48_rd_st(length)); | |
| output.push_back(knuth_b_time_st(length)); | |
| output.push_back(knuth_b_rd_st(length)); | |
| output.push_back(default_random_engine_time_st(length)); | |
| output.push_back(default_random_engine_rd_st(length)); | |
| output.push_back(random_device_st(length)); | |
| struct { | |
| bool operator()(std::string a, std::string b) const { | |
| auto astartpos = a.find('=') + 2; | |
| auto alength = a.find('[') - astartpos; | |
| auto asubstr = a.substr(astartpos, alength); | |
| auto bstartpos = b.find('=') + 2; | |
| auto blength = b.find('[') - bstartpos; | |
| auto bsubstr = b.substr(bstartpos, blength); | |
| try { | |
| return std::stoll(asubstr) < std::stoll(bsubstr); | |
| } | |
| catch (const std::exception& e) { | |
| std::cout << e.what() << std::endl; | |
| std::terminate(); | |
| } | |
| } | |
| } customLess; | |
| std::sort(output.begin(), output.end(), customLess); | |
| int longeststr = 0; | |
| for (auto i : output) | |
| if (i.find('[') > longeststr) | |
| longeststr = i.find('['); | |
| for (auto i : output) { | |
| while (longeststr > i.find('[')) { | |
| i.insert(i.begin() + i.find('=') + 2, ' '); | |
| } | |
| std::cout << i; | |
| } | |
| std::cout << std::endl; | |
| } | |
| int main() { | |
| short LITTLElength = 8; | |
| unsigned long long biglength = std::pow(2, 24); | |
| show_diff(LITTLElength); | |
| show_diff(biglength); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment