Skip to content

gh-91851: Trivial optimizations in Fraction - #100791

Merged
mdickinson merged 3 commits into
python:mainfrom
skirpichev:micro-opt-fractions-1
Jan 6, 2023
Merged

gh-91851: Trivial optimizations in Fraction#100791
mdickinson merged 3 commits into
python:mainfrom
skirpichev:micro-opt-fractions-1

Conversation

@skirpichev

@skirpichevskirpichev commented Jan 6, 2023

Copy link
Copy Markdown
Member

@hauntsaninjahauntsaninja changed the title Trivial optimizations in Fractiongh-91851: Trivial optimizations in FractionJan 6, 2023

@hauntsaninjahauntsaninja left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! Microbenchmarks confirm that this is about 2x as fast.

cc @JelleZijlstra

@mdickinson

mdickinson commented Jan 6, 2023

Copy link
Copy Markdown
Member

LGTM.

That's a pretty clunky implementation of round-ties-to-even in the ndigits is None case. Would something like the following be faster?

d=self._denominatorrounded, remainder=divmod(self._numerator+ (d>>1), d)
ifnotremainderandnot (d&1): # tie caserounded&=-2returnrounded

EDIT: A bit more streamlined, and avoiding unnecessary not operations:

d=self._denominatorrounded, remainder=divmod(self._numerator+ (d>>1), d)
returnroundedifremainderord&1elserounded&-2

Comment threadMisc/NEWS.d/next/Library/2023-01-05-23-04-15.gh-issue-91851.AuCzU5.rst Outdated
@mdickinson

Copy link
Copy Markdown
Member

Would something like the following be faster?

Turns out not, at least on my machine.

@mdickinson
mdickinson merged commit 0e64026 into python:mainJan 6, 2023
@skirpichev
skirpichev deleted the micro-opt-fractions-1 branch January 6, 2023 16:07
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@skirpichev@mdickinson@hauntsaninja@AlexWaygood@bedevere-bot