Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use faster XOR shift RNG. #9

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Conversation

tritoke
Copy link
Contributor

@tritoke tritoke commented Feb 5, 2021

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
Copy link
Contributor Author

tritoke commented Feb 5, 2021

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

@morisil
Copy link
Member

morisil commented Feb 5, 2021

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.

@tritoke
Copy link
Contributor Author

tritoke commented Feb 5, 2021

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
Copy link
Contributor Author

tritoke commented Feb 15, 2021

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.

@morisil
Copy link
Member

morisil commented Feb 15, 2021

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

@tritoke tritoke changed the title WIP: use faster XOR shift RNG. use faster XOR shift RNG. Mar 21, 2021
@tritoke
Copy link
Contributor Author

tritoke commented Mar 21, 2021

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
Copy link
Contributor Author

tritoke commented Mar 21, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants