gh-130167: Improve speed of _pydecimal._all_zeros and _pydecimal._exact_half by replacing re#132065
gh-130167: Improve speed of _pydecimal._all_zeros and _pydecimal._exact_half by replacing re#132065Marius-Juston wants to merge 10 commits intopython:mainfrom
_pydecimal._all_zeros and _pydecimal._exact_half by replacing re#132065Conversation
|
I have some doubts this is worthwhile. The |
Lib/_pydecimal.py
Outdated
| _all_zeros = re.compile('0*$').match | ||
| _exact_half = re.compile('50*$').match | ||
| # Checks for regex 0*$ | ||
| def _all_zeros(d_int,prec=0): |
There was a problem hiding this comment.
Do we need the prec argument?
There was a problem hiding this comment.
yes we need it, since when it gets called in:
def _round_down(self, prec):
"""Also known as round-towards-0, truncate."""
if _all_zeros(self._int, prec):
return 0
else:
return -1and
def _round_half_up(self, prec):
"""Rounds 5 up (away from 0)"""
if self._int[prec] in '56789':
return 1
elif _all_zeros(self._int, prec):
return 0
else:
return -1they both need the argument
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
with the new while loop ( should have done this method first lol ) |
With the new while loop method for the |
This is probably not a bottleneck in actual computation ( it's a nice and easy optimization for sure ) and i was planning to use public methods later for benchmarking ( separating them this way though made it simpler to optimize individually ). And yeah I was planning to do larger numbers as well to test this out with repeating patterns as well to see. ( though since the prec sets the initial location of the Though these addition benchmarks will take me some time to do since I will be very busy, so if you wanted to test it yourself feel free! |
|
I don't think this change needs to be done. |
If so, hardly it's a "nice optimization". E.g. the re dependency is not removed. BTW, what's you use case for the pure-Python decimal module? In most cases, people will just use C-coded extension. |
|
Fair enough, I don't actually have a use case for the _pydecimal I was just looking through files and functions to see if there was anything that interested me |
Benchmark for all zeros
Benchmark for exact half
The tests benchmarks
reuses #130167