Skip to content

Conversation

@skirpichev
Copy link
Member

@skirpichevskirpichev commented Jan 6, 2023

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

@hauntsaninjahauntsaninja left a comment

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
Copy link
Member

mdickinson commented Jan 6, 2023

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

@mdickinson
Copy link
Member

Would something like the following be faster?

Turns out not, at least on my machine.

@mdickinsonmdickinson merged commit 0e64026 into python:mainJan 6, 2023
@skirpichevskirpichev 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