Skip to content

gh-72902: improve Fraction constructor speed for typical inputs - #134320

Merged
serhiy-storchaka merged 1 commit into
python:mainfrom
skirpichev:speedup-Fraction
May 20, 2025
Merged

gh-72902: improve Fraction constructor speed for typical inputs#134320
serhiy-storchaka merged 1 commit into
python:mainfrom
skirpichev:speedup-Fraction

Conversation

@skirpichev

@skirpichevskirpichev commented May 20, 2025

Copy link
Copy Markdown
Member

This moves abc check for numbers.Rational - down.


Benchmarkrefpatch
Fraction(myint)5.62 us7.03 us: 1.25x slower
Fraction(Fraction(1, 2))5.89 us4.88 us: 1.21x faster
Fraction(Decimal('0.25'))11.1 us7.98 us: 1.39x faster
Fraction(IntSubclass(123))5.93 us5.05 us: 1.17x faster
Fraction(FloatSubclass(0.25))7.69 us4.26 us: 1.81x faster
Fraction('123')19.2 us16.1 us: 1.19x faster
Fraction('1/3')19.6 us16.4 us: 1.20x faster
Fraction('1.2e-3')26.1 us23.0 us: 1.14x faster
Fraction('-.2')23.4 us20.2 us: 1.16x faster
Geometric mean(ref)1.21x faster
Details
# bench.pyimportpyperffromfractionsimportFractionasFfromdecimalimportDecimalasDfromnumbersimportIntegralrunner=pyperf.Runner()
s='Fraction'classMyInt:
numerator=123denominator=1def__int__(self):
return123def__repr__(self):
return"myint"classIntSubclass(int):
def__repr__(self):
return'IntSubclass('+super().__repr__()+')'classFloatSubclass(float):
def__repr__(self):
return'FloatSubclass('+super().__repr__()+')'Integral.register(MyInt)
forvin [MyInt(), F(1, 2), D("0.25"),
IntSubclass(123), FloatSubclass(0.25),
"123", "1/3", "1.2e-3", "-.2"]:
r=s+'('+repr(v) +')'runner.bench_func(r, F, v)

This moves abc check for numbers.Rational - down.
@eendebakpt

Copy link
Copy Markdown
Contributor

There is a slight behaviour change here: for Fraction subclasses the construction now goes via .as_integer_ratio, instead of via .numerator and .denumerator (for this reason the benchmark Fraction(Fraction(1, 2)) is improving).

A minimal example:

import fractions
class MyFraction(Fraction):
@property
def numerator(self):
print('he!')
return super().numerator
f = MyFraction(1,2)
Fraction(f)

I think the change is acceptable and would probably accept the PR.

@skirpichev

Copy link
Copy Markdown
MemberAuthor
>>> help(fractions.Fraction.as_integer_ratio)
Help on function as_integer_ratio in module fractions:
as_integer_ratio(self)
Return a pair of integers, whose ratio is equal to the original Fraction.
The ratio is in lowest terms and has a positive denominator.

@serhiy-storchakaserhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. 👍

@serhiy-storchaka
serhiy-storchaka merged commit 175ba36 into python:mainMay 20, 2025
@skirpichev
skirpichev deleted the speedup-Fraction branch May 20, 2025 11:02
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
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.

3 participants

@skirpichev@eendebakpt@serhiy-storchaka