Skip to content
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

gh-93476: Fraction.limit_denominator speed increase #93477

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,11 @@ def limit_denominator(self, max_denominator=1000000):
if max_denominator < 1:
raise ValueError("max_denominator should be at least 1")
if self._denominator <= max_denominator:
return Fraction(self)
return Fraction(self._numerator, self._denominator, _normalize=False)
Copy link
Member

Choose a reason for hiding this comment

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

Does this change make a significant difference to performance? If not, I'd suggest reverting it, both for readability and for the purposes of keeping the PR focused. From looking at the code, a simple Fraction(self) shouldn't end up doing any gcd calculation here.

Copy link
Author

Choose a reason for hiding this comment

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

It actually saves 30% on this case based on tests above -- however, I hadn't realized at the time that the return self was executed in this branch and no normalization was done. The speed increase is probably based on the isinstance() check, which just keeps getting faster and faster in subsequent versions, so it might eventually be the faster route. I've removed the change.

If Fractions are eventually to be truly immutable and hashable, then return self is the correct response here, but that is probably backwards incompatible with real code where people change _numerator or _denominator after instantiation.


p0, q0, p1, q1 = 0, 1, 1, 0
n, d = self._numerator, self._denominator
n_original, d_original = n, d
while True:
a = n//d
q2 = q0+a*q1
Expand All @@ -247,12 +248,23 @@ def limit_denominator(self, max_denominator=1000000):
n, d = d, n-a*d

k = (max_denominator-q0)//q1
bound1 = Fraction(p0+k*p1, q0+k*q1)
bound2 = Fraction(p1, q1)
if abs(bound2 - self) <= abs(bound1-self):
return bound2
bound1_n = p0 + k*p1
bound1_d = q0 + k*q1
bound2_n = p1
bound2_d = q1
bound1_minus_self_n = abs((bound1_n * d_original)
- (n_original * bound1_d))
bound1_minus_self_d = d_original * bound1_d
bound2_minus_self_n = abs((bound2_n * d_original)
- (n_original * bound2_d))
bound2_minus_self_d = d_original * bound2_d

difference = ((bound1_minus_self_n * bound2_minus_self_d)
- (bound2_minus_self_n * bound1_minus_self_d))
if difference >= 0:
return Fraction(bound2_n, bound2_d)
else:
return bound1
return Fraction(bound1_n, bound1_d)

@property
def numerator(a):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:meth:`fractions.Fraction.limit_denominator()` performance enhancements.
Patch by Michael Scott Asato Cuthbert.