Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2025

Conversation

skirpichev
Copy link
Member

@skirpichev skirpichev commented May 20, 2025

This moves abc check for numbers.Rational - down.


Benchmark ref patch
Fraction(myint) 5.62 us 7.03 us: 1.25x slower
Fraction(Fraction(1, 2)) 5.89 us 4.88 us: 1.21x faster
Fraction(Decimal('0.25')) 11.1 us 7.98 us: 1.39x faster
Fraction(IntSubclass(123)) 5.93 us 5.05 us: 1.17x faster
Fraction(FloatSubclass(0.25)) 7.69 us 4.26 us: 1.81x faster
Fraction('123') 19.2 us 16.1 us: 1.19x faster
Fraction('1/3') 19.6 us 16.4 us: 1.20x faster
Fraction('1.2e-3') 26.1 us 23.0 us: 1.14x faster
Fraction('-.2') 23.4 us 20.2 us: 1.16x faster
Geometric mean (ref) 1.21x faster
# bench.py
import pyperf
from fractions import Fraction as F
from decimal import Decimal as D
from numbers import Integral

runner = pyperf.Runner()
s = 'Fraction'
class MyInt:
    numerator = 123
    denominator = 1
    def __int__(self):
        return 123
    def __repr__(self):
        return "myint"
class IntSubclass(int):
    def __repr__(self):
        return 'IntSubclass('+super().__repr__()+')'
class FloatSubclass(float):
    def __repr__(self):
        return 'FloatSubclass('+super().__repr__()+')'

Integral.register(MyInt)
for v in [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: 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
Member Author

>>> 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-storchaka serhiy-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-storchaka serhiy-storchaka merged commit 175ba36 into python:main May 20, 2025
45 checks passed
@skirpichev skirpichev deleted the speedup-Fraction branch May 20, 2025 11:02
Sign up for free to 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