-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
Increase speed of Fractions.limit_denominator #93476
Copy link
Copy link
Closed
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
Metadata
Metadata
Assignees
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
Feature or enhancement
Speed up Fractions.limit_denominator by not creating intermediary Fraction objects
Pitch
Fractions.limit_denominatorreturns 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 - selfandbound2 - shelf) which are created just for getting their absolute values.Similar code speedups have been part of the
music21library 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 ofFraction(self)for a 23% speedup.PR to follow.