java-2-times-faster-than-c icon indicating copy to clipboard operation
java-2-times-faster-than-c copied to clipboard

use faster XOR shift RNG.

Open tritoke opened this issue 4 years ago • 7 comments

This pull request is to show how moving to a more traditional xorshift RNG can result in massive speedups as well as more consistent results across languages as it doesn't depend on floating point implementations, only on having a 64 bit int type with some bit-shifting operations.

This initial commit contains two new java files and 2 new C files. One benchmark each for the RNG, and another for the full system using the faster RNG.

The actual RNG implemented is xorshift*.

I'm going to add more languages to this pull request as and when I can.

tritoke avatar Feb 05 '21 16:02 tritoke

As well as being around 35x faster: image

The XORshift* RNG also gives the same results across all the languages I've added so far! C, Java, Rust and Go:

image

tritoke avatar Feb 05 '21 18:02 tritoke

As well as being around 35x faster: image

The XORshift* RNG also gives the same results across all the languages I've added so far! C, Java, Rust and Go:

image

Amazing, if it is so much faster, I would consider raising the default amount of repetitions a bit, to also account for runtime startup time for java and .net.

morisil avatar Feb 05 '21 21:02 morisil

Yeah that's definitely something to consider, I might run some experiments tomorrow to see what settings might make sense. I would aim for a runtime of ~20 seconds as reasonable for the fastest program?

tritoke avatar Feb 05 '21 21:02 tritoke

Sorry I've been a bit slow at working on this recently, I've just restarted my uni term and I'm in doing a game jam with some friends so it might be a bit until I've finished this PR.

tritoke avatar Feb 15 '21 10:02 tritoke

@tritoke Don't worry, there is no deadline for this research project :)

morisil avatar Feb 15 '21 11:02 morisil

So I have finally gotten round to adding kotlin, csharp and javascript versions. I haven't migrated the javascript version's main program, I have just added the xorshift RNG test to it. This is because it is incredibly slow in javascript. I believe this would be an interesting place to investigate, C compiled node modules and WASM for the pure JS version.

Other than that, I haven't changed the parameters of the main runs, and the random number generator tests all run between 2 and 5 seconds, with javascript taking more than 9 minutes on my machine...

I did find a xorshift128+ generator for javascript which took ~30 seconds to run the test, but that is still far, far slower than the others 🤷

Other PRNGs that seemed interesting to me in my research were the xoshiro and xoroshiro family of generators.

tritoke avatar Mar 21 '21 13:03 tritoke

image These are the benchmarks I could gather on my system. I don't have kotlin-nativec and can only run kotlin from IntelliJ so i don't have a good benchmark for it, I know it is similarly performant to java though.

tritoke avatar Mar 21 '21 13:03 tritoke