Skip to content

<random>: Fast discard() for minstd_rand and minstd_rand0 via fast exponentiation #5412

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

Merged
merged 5 commits into from
Apr 22, 2025

Conversation

mirounga
Copy link
Contributor

discard() minstd_rand and minstd_rand0 now takes logarithmic time
due to fast exponentiation and avoiding constant divisions.

@mirounga mirounga requested a review from a team as a code owner April 17, 2025 13:48
@github-project-automation github-project-automation bot moved this to Initial Review in STL Code Reviews Apr 17, 2025
@StephanTLavavej StephanTLavavej added the performance Must go faster label Apr 17, 2025
@StephanTLavavej StephanTLavavej self-assigned this Apr 17, 2025
@StephanTLavavej StephanTLavavej changed the title Fast discard() for minstd_rand and minstd_rand0 via fast exponentiation <random>: Fast discard() for minstd_rand and minstd_rand0 via fast exponentiation Apr 17, 2025
@StephanTLavavej StephanTLavavej removed their assignment Apr 17, 2025
@StephanTLavavej StephanTLavavej moved this from Initial Review to Ready To Merge in STL Code Reviews Apr 17, 2025
@StephanTLavavej
Copy link
Member

Thanks! 😻 I cleaned up a mistakenly added file, added a benchmark, and got the following hilarious results on my 5950X:

Benchmark Before After Speedup
BM_discard<std::minstd_rand>/0 1.68 ns 1.69 ns 0.99
BM_discard<std::minstd_rand>/1 3.32 ns 2.93 ns 1.13
BM_discard<std::minstd_rand>/8 14.8 ns 5.99 ns 2.47
BM_discard<std::minstd_rand>/64 107 ns 9.52 ns 11.24
BM_discard<std::minstd_rand>/512 843 ns 16.6 ns 50.78
BM_discard<std::minstd_rand>/4096 6805 ns 19.0 ns 358.16
BM_discard<std::minstd_rand>/32768 53885 ns 24.6 ns 2190.45
BM_discard<std::minstd_rand>/262144 430735 ns 29.1 ns 14801.89

Skipping an exponential amount of work is pretty awesome.

@StephanTLavavej StephanTLavavej moved this from Ready To Merge to Merging in STL Code Reviews Apr 22, 2025
@StephanTLavavej
Copy link
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit 720b305 into microsoft:main Apr 22, 2025
39 checks passed
@github-project-automation github-project-automation bot moved this from Merging to Done in STL Code Reviews Apr 22, 2025
@StephanTLavavej
Copy link
Member

StephanTLavavej commented Apr 22, 2025

Thanks for perhaps the biggest speedup in the STL ever! ⏱️ 😻 🐱

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants