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 — Optimize creation of unnormalized Fraction's in private methods (arithmetics, etc) · Issue #101773 · python/cpython

Optimize creation of unnormalized Fraction's in private methods (arithmetics, etc) #101773

New issue
Copy link
New issue
Copy link
Closed
Closed
Optimize creation of unnormalized Fraction's in private methods (arithmetics, etc)#101773
Copy link
Labels
performancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@skirpichev
skirpichev
opened on Feb 10, 2023
Issue body actions

Right now we just use __new__() with a private kwarg _normalize=False. That's slightly less efficient, then a dedicated class method.

POC patch

diff --git a/Lib/fractions.py b/Lib/fractions.py
index 49a3f2841a..27c75785fb 100644
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -315,6 +315,13 @@ def from_decimal(cls, dec):
                 (cls.__name__, dec, type(dec).__name__))
         return cls(*dec.as_integer_ratio())
 
+    @classmethod
+    def _from_pair(cls, num, den):
+        obj = super(Fraction, cls).__new__(cls)
+        obj._numerator = num
+        obj._denominator = den
+        return obj
+
     def is_integer(self):
         """Return True if the Fraction is an integer."""
         return self._denominator == 1
@@ -703,13 +710,13 @@ def _add(a, b):
         nb, db = b._numerator, b._denominator
         g = math.gcd(da, db)
         if g == 1:
-            return Fraction(na * db + da * nb, da * db, _normalize=False)
+            return Fraction._from_pair(na * db + da * nb, da * db)
         s = da // g
         t = na * (db // g) + nb * s
         g2 = math.gcd(t, g)
         if g2 == 1:
-            return Fraction(t, s * db, _normalize=False)
-        return Fraction(t // g2, s * (db // g2), _normalize=False)
+            return Fraction._from_pair(t, s * db)
+        return Fraction._from_pair(t // g2, s * (db // g2))
 
     __add__, __radd__ = _operator_fallbacks(_add, operator.add)
 

We can drop private kwarg of __new__() after adopting this way.

Some benchmarks

With above patch

$ ./python -m timeit -s 'from fractions import Fraction as Q' -s 'a,b=Q(3,7),Q(5, 8)' -u usec 'a+b'
50000 loops, best of 5: 4.4 usec per loop
$ ./python -m timeit -s 'from fractions import Fraction as Q' -s 'a,b=Q(3,7)**100,Q(5, 8)**100' -u usec 'a+b'
20000 loops, best of 5: 12.6 usec per loop

On the mastermain:

$ ./python -m timeit -s 'from fractions import Fraction as Q' -s 'a,b=Q(3,7),Q(5, 8)' -u usec 'a+b'
50000 loops, best of 5: 6.99 usec per loop
$ ./python -m timeit -s 'from fractions import Fraction as Q' -s 'a,b=Q(3,7)**100,Q(5, 8)**100' -u usec 'a+b'
20000 loops, best of 5: 15.9 usec per loop

Linked PRs

  • gh-101773: Optimize creation of Fractions in private methods #101780

Activity

  1. skirpichev
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Feb 10, 2023
  2. arhadthedev
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Feb 10, 2023
  3. skirpichev
    added 2 commits that reference this issue on Feb 10, 2023

    pythongh-101773: Optimize creation of Fraction's in private methods

    130db33

    pythongh-101773: Optimize creation of Fraction's in private methods

    9cc62f7
  4. mdboom
    added
    performancePerformance or resource usagePerformance or resource usage
    on Feb 10, 2023
  5. mdickinson
    added a commit that references this issue on Feb 27, 2023

    gh-101773: Optimize creation of Fractions in private methods (#101780)

    4f3786b
  6. mdickinson commented on Feb 27, 2023

    @mdickinson
    mdickinson
    on Feb 27, 2023
    Member
    More actions

    Done in #101780.

  7. mdickinson
    closed this as completedon Feb 27, 2023
  8. carljm
    added a commit that references this issue on Feb 28, 2023

    Merge branch 'main' into inlinecomp2

    73dc0ed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.