GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-72902: improve Fraction constructor speed for typical inputs by skirpichev · Pull Request #134320 · python/cpython

Conversation

@skirpichev

@skirpichev skirpichev commented May 20, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

This moves abc check for numbers.Rational - down.

  • Issue: Fractions instantiation revisited #72902

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)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@skirpichev
pythongh-72902: improve Fraction constructor speed for typical inputs
1c67935
This moves abc check for numbers.Rational - down.
@bedevere-app bedevere-app Bot mentioned this pull request May 20, 2025
Fractions instantiation revisited #72902
Open
@bedevere-app bedevere-app Bot added the awaiting review label May 20, 2025
@skirpichev skirpichev mentioned this pull request May 20, 2025
gh-72902: improve Fraction(str) speed (don't use regexp's) #133994
Closed
@eendebakpt

eendebakpt commented May 20, 2025

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev

skirpichev commented May 20, 2025

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.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka approved these changes May 20, 2025
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM. 👍

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels May 20, 2025
@serhiy-storchaka
serhiy-storchaka merged commit 175ba36 into python:main May 20, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label May 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
@skirpichev @Pranjal095
pythongh-72902: improve Fraction constructor speed for typical inputs (…
a04a7c9
…pythonGH-134320)

This moves abc check for numbers.Rational - down.
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
@skirpichev @taegyunkim
pythongh-72902: improve Fraction constructor speed for typical inputs (…
3bacecf
…pythonGH-134320)

This moves abc check for numbers.Rational - down.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@skirpichev @eendebakpt @serhiy-storchaka
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.