Skip to content

Add a minor Fraction.__hash__() optimization #15313

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 3 commits into from
Aug 17, 2019

Conversation

tim-one
Copy link
Member

@tim-one tim-one commented Aug 16, 2019

Add a minor Fraction.__hash__() optimization that got lost in the shuffle.

Document the optimizations.

@tim-one tim-one changed the title Add a minor optimization that got lost in the shuffle. Add a minor Fraction.__hash__() optimization Aug 16, 2019
@tim-one
Copy link
Member Author

tim-one commented Aug 16, 2019

Replacing the explicit mod with another call to hash() saves a little time because the int hash doesn't need to do division at all. Because optimizations already done put a small bound on the bit width of the integer to be reduced, this saves just a little time regardless of how large that int is. In percentage terms, it's most significant for small fractions, where the final division accounts for the largest percentage of total runtime. For Fraction(1243, 5813):

before: 200000 loops, best of 11: 1.92 usec per loop
   after: 200000 loops, best of 11: 1.78 usec per loop

@tim-one tim-one self-assigned this Aug 16, 2019
@tim-one tim-one requested a review from mdickinson August 16, 2019 15:39
@tim-one tim-one force-pushed the more-fraction-hash branch from 2a7e519 to 8beb39c Compare August 16, 2019 21:40
@tim-one tim-one merged commit 29bb227 into python:master Aug 17, 2019
@tim-one tim-one deleted the more-fraction-hash branch August 17, 2019 02:09
lisroach pushed a commit to lisroach/cpython that referenced this pull request Sep 10, 2019
* Add a minor `Fraction.__hash__` optimization that got lost in the shuffle.

Document the optimizations.
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
* Add a minor `Fraction.__hash__` optimization that got lost in the shuffle.

Document the optimizations.
websurfer5 pushed a commit to websurfer5/cpython that referenced this pull request Jul 20, 2020
* Add a minor `Fraction.__hash__` optimization that got lost in the shuffle.

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

Successfully merging this pull request may close these issues.

5 participants