From 4e2752b41b61d43390a2bc788c680aa2204a94cb Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 22 Mar 2021 09:28:21 +0300 Subject: [PATCH 1/3] Trivial optimizations in Fraction: use private class attributes --- Lib/fractions.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Lib/fractions.py b/Lib/fractions.py index 4302f3f1b98dea..939741115f9192 100644 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -650,12 +650,12 @@ def __trunc__(a): def __floor__(a): """math.floor(a)""" - return a.numerator // a.denominator + return a._numerator // a._denominator def __ceil__(a): """math.ceil(a)""" # The negations cleverly convince floordiv to return the ceiling. - return -(-a.numerator // a.denominator) + return -(-a._numerator // a._denominator) def __round__(self, ndigits=None): """round(self, ndigits) @@ -663,10 +663,11 @@ def __round__(self, ndigits=None): Rounds half toward even. """ if ndigits is None: - floor, remainder = divmod(self.numerator, self.denominator) - if remainder * 2 < self.denominator: + d = self._denominator + floor, remainder = divmod(self._numerator, d) + if remainder * 2 < d: return floor - elif remainder * 2 > self.denominator: + elif remainder * 2 > d: return floor + 1 # Deal with the half case: elif floor % 2 == 0: From 6df327f95a2d3e137432399a2d484d13d8906666 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 5 Jan 2023 23:04:17 -0800 Subject: [PATCH 2/3] news --- .../next/Library/2023-01-05-23-04-15.gh-issue-91851.AuCzU5.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-01-05-23-04-15.gh-issue-91851.AuCzU5.rst diff --git a/Misc/NEWS.d/next/Library/2023-01-05-23-04-15.gh-issue-91851.AuCzU5.rst b/Misc/NEWS.d/next/Library/2023-01-05-23-04-15.gh-issue-91851.AuCzU5.rst new file mode 100644 index 00000000000000..9bbd3c86251444 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-01-05-23-04-15.gh-issue-91851.AuCzU5.rst @@ -0,0 +1,3 @@ +Microoptimizations for :func:`fractions.Fraction.__round__`, +:func:`fractions.Fraction.__ceil__` and +:func:`fractions.Fraction.__floor__`. From f5d3a7dc2483b75be664621f0b6ed2f3a59f5fc8 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Fri, 6 Jan 2023 18:07:52 +0300 Subject: [PATCH 3/3] func -> meth --- .../Library/2023-01-05-23-04-15.gh-issue-91851.AuCzU5.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2023-01-05-23-04-15.gh-issue-91851.AuCzU5.rst b/Misc/NEWS.d/next/Library/2023-01-05-23-04-15.gh-issue-91851.AuCzU5.rst index 9bbd3c86251444..f427e8ae6791f0 100644 --- a/Misc/NEWS.d/next/Library/2023-01-05-23-04-15.gh-issue-91851.AuCzU5.rst +++ b/Misc/NEWS.d/next/Library/2023-01-05-23-04-15.gh-issue-91851.AuCzU5.rst @@ -1,3 +1,3 @@ -Microoptimizations for :func:`fractions.Fraction.__round__`, -:func:`fractions.Fraction.__ceil__` and -:func:`fractions.Fraction.__floor__`. +Microoptimizations for :meth:`fractions.Fraction.__round__`, +:meth:`fractions.Fraction.__ceil__` and +:meth:`fractions.Fraction.__floor__`.