diff --git a/Lib/_pydecimal.py b/Lib/_pydecimal.py index ec036199331396..dc55fdfa3c8dd1 100644 --- a/Lib/_pydecimal.py +++ b/Lib/_pydecimal.py @@ -6076,8 +6076,29 @@ def _convert_for_comparison(self, other, equality_op=False): \Z """, re.VERBOSE | re.IGNORECASE).match -_all_zeros = re.compile('0*$').match -_exact_half = re.compile('50*$').match +# Checks for regex 0*$ +def _all_zeros(d_int, prec=0): + i = prec + len_d = len(d_int) + + while i < len_d: + if d_int[i] != '0': + return False + i += 1 + return True + +# Checks for regex 50*$ +def _exact_half(d_int, prec=0): + len_d = len(d_int) + i = prec + 1 + + if len_d >= i and d_int[prec] == '5': + while i < len_d: + if d_int[i] != '0': + return False + i += 1 + return True + return False ##### PEP3101 support functions ############################################## # The functions in this section have little to do with the Decimal diff --git a/Misc/NEWS.d/next/Library/2025-04-04-04-07-16.gh-issue-130167.XigAlq.rst b/Misc/NEWS.d/next/Library/2025-04-04-04-07-16.gh-issue-130167.XigAlq.rst new file mode 100644 index 00000000000000..631a89ccfd1bc2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-04-04-04-07-16.gh-issue-130167.XigAlq.rst @@ -0,0 +1,2 @@ +Improved performance of :func:`_pydecimal._all_zeros` by an average of +~1.7x and :func:`_pydecimal._exact_half` by ~1.38x. Patch by Marius Juston