Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
serhiy-storchaka merged 1 commit intopython:mainfromskirpichev:speedup-Fraction
May 20, 2025

Conversation

skirpichev
Copy link
Member

@skirpichevskirpichev commentedMay 20, 2025
edited
Loading

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
# 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
Contributor

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

A minimal example:

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

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

@skirpichev
Copy link
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.

Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM. 👍

@serhiy-storchakaserhiy-storchaka merged commit175ba36 intopython:mainMay 20, 2025
45 checks passed
@skirpichevskirpichev deleted the speedup-Fraction branchMay 20, 2025 11:02
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@serhiy-storchakaserhiy-storchakaserhiy-storchaka approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@skirpichev@eendebakpt@serhiy-storchaka

[8]ページ先頭

©2009-2025 Movatter.jp