|
| 1 | +""" |
| 2 | +Copyright (c) 2025 Philippe Schmouker, schmouk (at) gmail.com |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in all |
| 12 | +copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +SOFTWARE. |
| 21 | +""" |
| 22 | + |
| 23 | +#============================================================================= |
| 24 | +import sys |
| 25 | +from time import perf_counter_ns |
| 26 | +from timeit import repeat |
| 27 | + |
| 28 | +from PyRandLib import * |
| 29 | + |
| 30 | + |
| 31 | +#============================================================================= |
| 32 | +def test_perf(prng_class_name: str, seed_value: int, n_loops: int, n_repeats: int): |
| 33 | + """Evaluates the CPU time spent evaluating a number in [0.0, 1.0).""" |
| 34 | + print("---", prng_class_name, "---") |
| 35 | + perfs = repeat("rnd()", |
| 36 | + setup=f"from PyRandLib import {prng_class_name}; rnd = {prng_class_name}({seed_value})", |
| 37 | + repeat=n_repeats, |
| 38 | + timer=perf_counter_ns, |
| 39 | + number=n_loops) |
| 40 | + print([1e-9 * p / n_loops for p in perfs]) |
| 41 | + print(f"--> {min(perfs) / n_loops * 1e-3:.4f} us\n") |
| 42 | + |
| 43 | + |
| 44 | +#============================================================================= |
| 45 | +if __name__ == "__main__": |
| 46 | + |
| 47 | + print("=== PyRandLib CPU time performances ===") |
| 48 | + print("Python version:", sys.version, '\n') |
| 49 | + |
| 50 | + N = 15 |
| 51 | + |
| 52 | + test_perf("FastRand32" , 0x3ca5_8796 , 2_000_000, N) |
| 53 | + test_perf("FastRand63" , 0x3ca5_8796_1f2e_b45a, 2_000_000, N) |
| 54 | + test_perf("LFib78" , 0x3ca5_8796_1f2e_b45a, 2_000_000, N) |
| 55 | + test_perf("LFib116" , 0x3ca5_8796_1f2e_b45a, 2_000_000, N) |
| 56 | + test_perf("LFib668" , 0x3ca5_8796_1f2e_b45a, 2_000_000, N) |
| 57 | + test_perf("LFib1340" , 0x3ca5_8796_1f2e_b45a, 2_000_000, N) |
| 58 | + test_perf("MRGRand287" , 0x3ca5_8796 , 2_000_000, N) |
| 59 | + test_perf("MRGRand1457" , 0x3ca5_8796 , 2_000_000, N) |
| 60 | + test_perf("MRGRand49507", 0x3ca5_8796 , 2_000_000, N) |
| 61 | + test_perf("Well512a" , 0x3ca5_8796 , 1_000_000, N) |
| 62 | + test_perf("Well1024a" , 0x3ca5_8796 , 1_000_000, N) |
| 63 | + test_perf("Well19937c" , 0x3ca5_8796 , 1_000_000, N) |
| 64 | + test_perf("Well44497b" , 0x3ca5_8796 , 1_000_000, N) |
| 65 | + |
| 66 | + |
| 67 | +#===== end of module testCPUPerfs.py =================================== |
0 commit comments