-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
Improve performance of random.randbelow by providing a C implementation #126149
Comments
-1 We've long resisted recoding the random module in C. |
Considering this answer, I assume that this issue is likely to be closed. However, I'm wondering: what was the reason of not recoding it in C? is it to avoid additional work and / or hard-to-spot corner cases? (or maybe performances were not sufficiently important?) |
We could code every Python module in C and make it faster. But we do like and prefer our own language which users can read and is easy for us to maintain. The module does have a C foundation, |
Keeping the code in Python also makes it possible for future optimizations such as work on the JIT to make this code faster "for free". |
I was just about to add that note as well. Pure python is getting faster and faster. |
Closing as |
Feature or enhancement
Proposal:
We can improve performance of
random.randbelow
(and methods depending on that functionality such asrandom.shuffle
) by creating a C implementation of_random._randbelow_with_getrandbits
. A quick prototype with a straightforward port of the Python code shows we can gain a factor 2 or better in performance for many cases.Benchmark:
Prototype: main...eendebakpt:randbelow_c
Benchmark script
@rhettinger In your opinion: is the speed-up from the C implementation worthwhile when compared to the additional complexity of the C implementation? If so I will cleanup the code and make a PR
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
The text was updated successfully, but these errors were encountered: