Skip to content

Commit 68b1737

Browse files
committed
Tweak _DIV_LIMIT and improve test.
Another optimization from Bjorn. Adjust threshold limits in longobject.c since the _pylong version is faster.
1 parent bd6cd70 commit 68b1737

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

Lib/_pylong.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def str_to_int(s):
168168
# algorithm is due to Burnikel and Ziegler, in their paper "Fast Recursive
169169
# Division".
170170

171-
_DIV_LIMIT = 1000
171+
_DIV_LIMIT = 4000
172172

173173

174174
def _div2n1n(a, b, n):
@@ -184,7 +184,7 @@ def _div2n1n(a, b, n):
184184
(q, r) such that a = b*q+r and 0 <= r < b.
185185
186186
"""
187-
if n <= _DIV_LIMIT:
187+
if a.bit_length() - n <= _DIV_LIMIT:
188188
return divmod(a, b)
189189
pad = n & 1
190190
if pad:

Objects/longobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4122,11 +4122,11 @@ l_divmod(PyLongObject *v, PyLongObject *w,
41224122
#if WITH_PYLONG_MODULE
41234123
Py_ssize_t size_v = Py_ABS(Py_SIZE(v)); /* digits in numerator */
41244124
Py_ssize_t size_w = Py_ABS(Py_SIZE(w)); /* digits in denominator */
4125-
if (size_w > 500 && (size_v - size_w) > 250) {
4125+
if (size_w > 300 && (size_v - size_w) > 150) {
41264126
/* Switch to _pylong.int_divmod(). If the quotient is small then
41274127
"schoolbook" division is linear-time so don't use in that case.
41284128
These limits are empirically determined and should be slightly
4129-
conservative so that _pylong gets used in cases it is likely
4129+
conservative so that _pylong is used in cases it is likely
41304130
to be faster. */
41314131
return pylong_int_divmod(v, w, pdiv, pmod);
41324132
}

0 commit comments

Comments
 (0)