Stream: helpdesk (published)

Topic: ✔ `Xoshiro` vs. `MersenneTwister`


view this post on Zulip Júlio Hoffimann (Jul 04 2025 at 15:21):

Do you have a general rule of thumb for picking one over the other?

Benchmarks are inconsistent depending on how many random numbers are generated:

julia> rng1 = MersenneTwister(123)
MersenneTwister(123)

julia> rng2 = Xoshiro(123)
Xoshiro(0xfefa8d41b8f5dca5, 0xf80cc98e147960c1, 0x20e2ccc17662fc1d, 0xea7a7dcb2e787c01, 0xf4e85a418b9c4f80)

julia> @btime rand($rng1, 1);
  27.570 ns (2 allocations: 64 bytes)

julia> @btime rand($rng2, 1);
  19.695 ns (2 allocations: 64 bytes)

julia> @btime rand($rng1, 100);
  118.183 ns (2 allocations: 928 bytes)

julia> @btime rand($rng2, 100);
  133.110 ns (2 allocations: 928 bytes)

view this post on Zulip Mason Protter (Jul 04 2025 at 15:29):

I just always use Xoshiro. MT is kinda just for backwards compatibility

view this post on Zulip Júlio Hoffimann (Jul 04 2025 at 15:31):

That is what I was told. I am curious why it is slower in the n=100 case above.

view this post on Zulip Mason Protter (Jul 04 2025 at 15:32):

FWIW, I don't see Xoshiro lose at any size on my machine:

julia> using Random

julia> rng1 = MersenneTwister(123)
MersenneTwister(123)

julia> rng2 = Xoshiro(123)
Xoshiro(0xfefa8d41b8f5dca5, 0xf80cc98e147960c1, 0x20e2ccc17662fc1d, 0xea7a7dcb2e787c01, 0xf4e85a418b9c4f80)

julia> @btime rand($rng1, 1);
  20.245 ns (2 allocations: 64 bytes)

julia> @btime rand($rng2, 1);
  15.435 ns (2 allocations: 64 bytes)

julia> @btime rand($rng1, 100);
  71.995 ns (2 allocations: 928 bytes)

julia> @btime rand($rng2, 100);
  66.703 ns (2 allocations: 928 bytes)

julia> @btime rand($rng1, 10000);
  3.859 μs (3 allocations: 78.20 KiB)

julia> @btime rand($rng2, 10000);
  2.639 μs (3 allocations: 78.20 KiB)

view this post on Zulip Mason Protter (Jul 04 2025 at 15:33):

I wouldn't be surprised if this sort of thing was pretty architecture dependent.

view this post on Zulip Júlio Hoffimann (Jul 04 2025 at 15:35):

Interesting. I will move to Xoshiro in that case.

view this post on Zulip Notification Bot (Jul 04 2025 at 15:35):

Júlio Hoffimann has marked this topic as resolved.

view this post on Zulip Jakob Nybo Nissen (Jul 04 2025 at 16:26):

IIRC, MT buffers a bunch of random numbers which xoshiro doesn't. So when you time with @btime, which gives the minimum time, you don't benchmark the actual time to generate the numbers, only that to copy it from the buffer

view this post on Zulip Júlio Hoffimann (Jul 04 2025 at 16:32):

Thanks for sharing.

view this post on Zulip Brenhin Keller (Jul 04 2025 at 22:07):

There's a whole funny rivalry between the Xoshiro and PCG people, but Mersenne is old and either is probably an upgrade over that IMHO


Last updated: Jul 22 2025 at 04:56 UTC