thread_rng
is a very heavyweight random number generator, but even then your benchmark shows something like a 10% discrepancy. If you need random numbers for a simulation or something like that, use SmallRng
instead.
Am I running it incorrectly? In the playground:
Compiling playground v0.0.1 (/playground)
Finished release [optimized] target(s) in 1.44s
Running `target/release/playground`
[src/main.rs:11] dt = 11.47068ms
[src/main.rs:11] dt = 12.598012ms
It's a "quick" benchmark and not very accurate. Try running it multiple times, and you'll notice that sometimes the first and sometimes the second result is greater.
@tguichaoua benchmark, in contrast, shows that calling thread_rng
explicitly once and then using the ThreadRng
(as Rng
) is faster when you want to generate a lot of random values.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.