GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Optimize creation of unnormalized Fraction's in private methods (arithmetics, etc) · Issue #101773 · python/cpython Closed Closed 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 DescriptionRight now we just use POC patchdiff --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 Some benchmarksWith above patch On the Linked PRsActivity
MetadataMetadataAssigneesLabelsperformancePerformance 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 ProjectsMilestoneRelationshipsDevelopmentIssue 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. |