Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tweak _DIV_LIMIT and improve test.
Another optimization from Bjorn.  Adjust threshold limits in
longobject.c since the _pylong version is faster.
  • Loading branch information
nascheme committed Oct 25, 2022
commit 68b17374aabcfd87a9aa35998dacc4463f07b0ca
4 changes: 2 additions & 2 deletions Lib/_pylong.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def str_to_int(s):
# algorithm is due to Burnikel and Ziegler, in their paper "Fast Recursive
# Division".

_DIV_LIMIT = 1000
_DIV_LIMIT = 4000


def _div2n1n(a, b, n):
Expand All @@ -184,7 +184,7 @@ def _div2n1n(a, b, n):
(q, r) such that a = b*q+r and 0 <= r < b.

"""
if n <= _DIV_LIMIT:
if a.bit_length() - n <= _DIV_LIMIT:
return divmod(a, b)
pad = n & 1
if pad:
Expand Down
4 changes: 2 additions & 2 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4122,11 +4122,11 @@ l_divmod(PyLongObject *v, PyLongObject *w,
#if WITH_PYLONG_MODULE
Py_ssize_t size_v = Py_ABS(Py_SIZE(v)); /* digits in numerator */
Py_ssize_t size_w = Py_ABS(Py_SIZE(w)); /* digits in denominator */
if (size_w > 500 && (size_v - size_w) > 250) {
if (size_w > 300 && (size_v - size_w) > 150) {
/* Switch to _pylong.int_divmod(). If the quotient is small then
"schoolbook" division is linear-time so don't use in that case.
These limits are empirically determined and should be slightly
conservative so that _pylong gets used in cases it is likely
conservative so that _pylong is used in cases it is likely
to be faster. */
return pylong_int_divmod(v, w, pdiv, pmod);
}
Expand Down