Increase speed of Fractions.limit_denominator #93476
Labels
3.12
bugs and security fixes
performance
Performance or resource usage
stdlib
Python modules in the Lib dir
type-feature
A feature request or enhancement
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
andbound2 - 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 ofFraction(self)
for a 23% speedup.PR to follow.
The text was updated successfully, but these errors were encountered: