Skip to content

bpo-45976: Remove unhelpful locals in Random._randbelow() #29911

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

Closed
wants to merge 2 commits into from
Closed

bpo-45976: Remove unhelpful locals in Random._randbelow() #29911

wants to merge 2 commits into from

Conversation

onethreeseven
Copy link

@onethreeseven onethreeseven commented Dec 4, 2021

_randbelow_with_getrandbits() calls getrandbits() at most twice on average, so
the local reference never pays for itself. Speeds up randrange(), shuffle(),
and sample().

_randbelow_without_getrandbits() gets a similar change for similar reasons.

https://bugs.python.org/issue45976

_randbelow_with_getrandbits() calls getrandbits() at most twice on average, so
the local reference never pays for itself.  Speeds up randrange(), shuffle(),
and sample().

_randbelow_without_getrandbits() gets a similar change for similar reasons.
@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

Recognized GitHub username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@onethreeseven

This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@rhettinger
Copy link
Contributor

Please sign the CLA and continue to update this PR.

I'll review it again when Python 3.11 is more mature, so we can have reliable before/after timings.

@rhettinger rhettinger added the performance Performance or resource usage label Dec 4, 2021
@onethreeseven
Copy link
Author

I think I've done what is necessary to sign the CLA and will check back on it in a few days.

@github-actions
Copy link

github-actions bot commented Jan 4, 2022

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Jan 4, 2022
Copy link
Contributor

@MaxwellDupre MaxwellDupre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to significantly hack the randbelow_timing.py to get it to work but still not right especially the function MainWithoutGetrandbits (random = self.random?). Hence, could you post a working test file? And possibly add a test?

@onethreeseven
Copy link
Author

This issue has been dormant for quite some time but I find it very unlikely that I would have posted an obviously broken test file. Are you using Python 3.10 or current main branch, as it says in the comment?

@onethreeseven
Copy link
Author

I would also note that if you find random = self.random unusual, your issue is with the current standard library, not with me. But that kind of optimization is the entire thrust of the PR.

@rhettinger
Copy link
Contributor

Marking this as closed. For the average case, it is mildly beneficial but only on some builds. For the multiple rejection case it is detrimental. I prefer to minimize the cost of the unhappy case.

@rhettinger rhettinger closed this Feb 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting core review DO-NOT-MERGE performance Performance or resource usage stale Stale PR or inactive for long period of time.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants