summaryrefslogtreecommitdiffstats
path: root/Lib/fractions.py
diff options
context:
space:
mode:
authorSergey B Kirpichev <skirpichev@gmail.com>2023-01-06 15:37:34 (GMT)
committerGitHub <noreply@github.com>2023-01-06 15:37:34 (GMT)
commit0e640260dac2db081e56f52f8efb0e43e463ff2f (patch)
tree529853d3506d63aeb8771f87420f9f1e76485e31 /Lib/fractions.py
parent15c44789bb125b93e96815a336ec73423c47508e (diff)
downloadcpython-0e640260dac2db081e56f52f8efb0e43e463ff2f.zip
cpython-0e640260dac2db081e56f52f8efb0e43e463ff2f.tar.gz
cpython-0e640260dac2db081e56f52f8efb0e43e463ff2f.tar.bz2
gh-91851: Trivial optimizations in Fraction (#100791)
Make some trivial performance optimizations in Fraction Uses private class attributes `_numerator` and `_denominator` in place of the `numerator` and `denominator` property accesses. Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
Diffstat (limited to 'Lib/fractions.py')
-rw-r--r--Lib/fractions.py11
1 files changed, 6 insertions, 5 deletions
diff --git a/Lib/fractions.py b/Lib/fractions.py
index 4302f3f..9397411 100644
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -650,12 +650,12 @@ class Fraction(numbers.Rational):
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 @@ class Fraction(numbers.Rational):
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: