Help needed: Fixing performance problems w/js_sys::Math::random #3196
-
Hey everybody, I'm looking for some help fixing a performance problem in some Rust WASM code I am writing. I'm building a simulation program that needs to calculate a ton of random numbers — I'm talking in the range of 16 to 144 million or more. I've benchmarked my code with the Chrome profiler, and it says that calls to Is there a way I can re-architect my code to have better performance? Is there an alternate random number generator that will be good for my use case? Any help would be greatly appreciated 😄 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You could give the # Cargo.toml
[dependencies]
rand = "0.8.5"
getrandom = { version = "0.2.8", features = ["js"] }
I haven't actually checked whether |
Beta Was this translation helpful? Give feedback.
You could give the
rand
crate a go, which includes random number generators written in Rust that could potentially be faster than usingMath.random
. To seed them from the browser's RNG, you'll need to enable thejs
feature of thegetrandom
crate, like so:Math.random
wouldn't be slow because of the JS glue code, though, because it doesn't have any - all it does is return a singlef64
, which wasm can accept directly without any glue.I haven't actually checked whether
rand
is faster. If you don't need a cryptographic RNG,rand::rngs::SmallRng
should also be faster than its default RNG.