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 — Increase speed of Fractions.limit_denominator · Issue #93476 · python/cpython

Increase speed of Fractions.limit_denominator #93476

New issue
Copy link
New issue
Copy link
Closed
Closed
Increase speed of Fractions.limit_denominator#93476
Copy link
Labels
3.12only security fixesonly security fixesperformancePerformance 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

@mscuthbert
mscuthbert
opened on Jun 3, 2022
Issue body actions

Feature or enhancement

Speed up Fractions.limit_denominator by not creating intermediary Fraction objects

Pitch

Fractions.limit_denominator returns a new Fraction, but along the way for Fractions > max_denominator creates 3 Fraction objects that are discarded: the fraction farther from the bound and two temporary fractions (bound1 - self and bound2 - shelf) which are created just for getting their absolute values.

Similar code speedups have been part of the music21 library since 2015--where we call limit_denominator after every arithmetic operation on note placement--and timing tests give a 7.3x speedup. (see music21 Source )

For fractions with denominators equal to or below max_denominator, instantiate the new Fraction with Fraction(self._numerator, self._denominator) instead of Fraction(self) for a 23% speedup.

PR to follow.

Activity

  1. mscuthbert
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Jun 3, 2022
  2. mscuthbert
    added a commit that references this issue on Jun 3, 2022

    pythongh-93476: Fraction.limit_denominator speed increases

    599c55b
  3. AlexWaygood
    added
    performancePerformance or resource usagePerformance or resource usage
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    3.12only security fixesonly security fixes
    on Jun 3, 2022
  4. rhettinger commented on Jun 5, 2022

    @rhettinger
    rhettinger
    on Jun 5, 2022
    Contributor
    More actions

    In this case, we value algorithmic clarity over a minor performance gain from inlining.

    Also, we would lose some of the recent optimizations in the Fraction class.

    The limit_denominator code is almost never in a performance critical section of code, so no one would notice the small benefit, but we would incur a loss of understanding about how it works.

    Thanks for the suggestion, but we will decline this one.

  5. rhettinger
    closed this as completedon Jun 5, 2022
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

    3.12only security fixesonly security fixesperformancePerformance 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.