Skip to content

Small clean-ups for the random module #21038

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 4 commits into from
Jun 23, 2020
Merged

Conversation

rhettinger
Copy link
Contributor

Timings:

$ python3.9 -m timeit -r 11 -s 'from math import floor' -s '_int = int' -s 'x=3.14' '_int(x)'
5000000 loops, best of 11: 68 nsec per loop
$ python3.9 -m timeit -r 11 -s 'from math import floor' -s '_int = int' -s 'x=3.14' 'floor(x)'
10000000 loops, best of 11: 36.7 nsec per loop

@rhettinger
Copy link
Contributor Author

The default argument _int = int is no longer of much value. The global variable lookup is now slightly cheaper than the cost of the extra default argument for the first lookup.

Baseline timings:

$ pytime -s 'from random import randrange' 'randrange(1000)'
500000 loops, best of 11: 559 nsec per loop
$ pytime -s 'from random import randrange' 'randrange(1000, 2000)'
500000 loops, best of 11: 744 nsec per loop
$ pytime -s 'from random import randrange' 'randrange(1000, 2000, 100)'
200000 loops, best of 11: 1.04 usec per loop

New timings:

$ pytime -s 'from random import randrange' 'randrange(1000)'
500000 loops, best of 11: 556 nsec per loop
$ pytime -s 'from random import randrange' 'randrange(1000, 2000)'
500000 loops, best of 11: 747 nsec per loop
$ pytime -s 'from random import randrange' 'randrange(1000, 2000, 100)'
200000 loops, best of 11: 1.05 usec per loop

* Timings should only include data generation and exclude analysis
* Can now use the statistics library
* Use f-strings for formatting
@rhettinger rhettinger changed the title Small optimization: floor() is faster and clearer than int() Small clean-ups for the random module Jun 22, 2020
@rhettinger rhettinger requested a review from tim-one June 22, 2020 20:39
Copy link
Member

@tim-one tim-one left a comment

Choose a reason for hiding this comment

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

Looks fine to me. Not sure what's up with int v floor - the latter is much faster (over factor of 3) for me under a current master build (Win64) than under the released 3.8.1.

@rhettinger rhettinger merged commit 26a1ad1 into python:master Jun 23, 2020
@miss-islington
Copy link
Contributor

Thanks @rhettinger for the PR 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-21065 is a backport of this pull request to the 3.9 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 23, 2020
(cherry picked from commit 26a1ad1)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
fasih pushed a commit to fasih/cpython that referenced this pull request Jun 29, 2020
arun-mani-j pushed a commit to arun-mani-j/cpython that referenced this pull request Jul 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage skip issue skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants