-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-130167: Improve speed of _pydecimal._all_zeros
and _pydecimal._exact_half
by replacing re
#132065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the prec
argument?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 -1
and
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 -1
they 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
re
uses #130167