| 1 | // Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | |
| 19 | #include <random> |
| 20 | #include <testsuite_performance.h> |
| 21 | |
| 22 | namespace counters |
| 23 | { |
| 24 | __gnu_test::time_counter time; |
| 25 | __gnu_test::resource_counter resource; |
| 26 | } |
| 27 | |
| 28 | |
| 29 | template<typename Dist, typename Engine> |
| 30 | void do_fill_with_uniform_ints(std::string name, Dist& d, Engine& e) |
| 31 | { |
| 32 | using counters::time; |
| 33 | using counters::resource; |
| 34 | |
| 35 | std::vector<typename Dist::result_type> r; |
| 36 | int n = 10000000; |
| 37 | { |
| 38 | const auto suffix = "-e" + std::to_string((int)std::log10(n)); |
| 39 | r.resize(n); |
| 40 | |
| 41 | start_counters(time, resource); |
| 42 | for (auto& x : r) |
| 43 | x = d(e); |
| 44 | stop_counters(time, resource); |
| 45 | report_performance(__FILE__, name+suffix, time, resource); |
| 46 | clear_counters(time, resource); |
| 47 | |
| 48 | d.reset(); |
| 49 | |
| 50 | start_counters(time, resource); |
| 51 | d.__generate(begin(r), end(r), e); |
| 52 | stop_counters(time, resource); |
| 53 | report_performance(__FILE__, name+"-range"+suffix, time, resource); |
| 54 | clear_counters(time, resource); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | template<typename Engine> |
| 59 | void fill_with_uniform_ints(std::string name, Engine& e) |
| 60 | { |
| 61 | using Dist = std::uniform_int_distribution<typename Engine::result_type>; |
| 62 | using param_type = typename Dist::param_type; |
| 63 | |
| 64 | unsigned maxima[]{6, 10, 32, 100, 1000, 1024, (1<<16)-1, 1<<16, 1<<20, -1u}; |
| 65 | for (auto hi : maxima) |
| 66 | { |
| 67 | Dist dist(param_type{0, hi}); |
| 68 | std::ostringstream s; |
| 69 | s << name << "-uniform_int-" << (dist.max() - dist.min()); |
| 70 | do_fill_with_uniform_ints(s.str(), dist, e); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | int main() |
| 75 | { |
| 76 | using namespace std; |
| 77 | |
| 78 | std::mt19937 mt; |
| 79 | fill_with_uniform_ints("mt19937", mt); |
| 80 | std::mt19937_64 mt64; |
| 81 | fill_with_uniform_ints("mt19937_64", mt64); |
| 82 | |
| 83 | // Same as std::mt19937 but using uint32_t not uint_fast32_t for result_type |
| 84 | using mt19937_32 = std::mersenne_twister_engine<uint32_t, 32, 624, 397, 31, |
| 85 | 0x9908b0df, 11, 0xffffffff, |
| 86 | 7, 0x9d2c5680, 15, |
| 87 | 0xefc60000, 18, 1812433253>; |
| 88 | mt19937_32 mt32; |
| 89 | fill_with_uniform_ints("mt19937_32", mt32); |
| 90 | |
| 91 | std::minstd_rand0 lcg; |
| 92 | fill_with_uniform_ints("minstd_rand0", lcg); |
| 93 | |
| 94 | // Same as std::minstd_rand0 but using uint32_t not uint_fast32_t |
| 95 | using minstd_rand0_32 = std::linear_congruential_engine<uint32_t, 48271, 0, |
| 96 | 2147483647>; |
| 97 | minstd_rand0_32 lcg_32; |
| 98 | fill_with_uniform_ints("minstd_rand0_32", lcg_32); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 |