FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-72902: improve Fraction constructor speed for typical inputs by skirpichev · Pull Request #134320 · python/cpython · GitHub

Repository navigation

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

serhiy-storchaka merged 1 commit into
python:mainfrom
skirpichev:speedup-Fraction

Conversation

skirpichev commented May 20, 2025 •
edited
Loading

Copy link
Copy Markdown
Member

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
Details
# 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.

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.

Copy link
Copy Markdown
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.

serhiy-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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM. 👍

serhiy-storchaka merged commit 175ba36 into python:main May 20, 2025
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL