STINNER Victor <[email protected]> added the comment:
PR 22479 avoids calling PyNumber_FloorDivide(a, b) if b == 1 (if b ==
_PyLong_One): it makes range.index(a, 1) call 214 ns faster. I'm surprised that
PyNumber_FloorDivide(a, 1) takes 214 ns. Does it spend most time in
binary_op1()? Or PyNumber_FloorDivide()?
long_div(a, 1) is quite simple:
CHECK_BINOP(a, b);
if (Py_ABS(Py_SIZE(a)) == 1 && Py_ABS(Py_SIZE(b)) == 1) {
return fast_floor_div((PyLongObject*)a, (PyLongObject*)b);
with:
static PyObject *
fast_floor_div(PyLongObject *a, PyLongObject *b)
{
sdigit left = a->ob_digit[0];
sdigit right = b->ob_digit[0];
sdigit div;
if (Py_SIZE(a) == Py_SIZE(b)) {
div = left / right;
}
else {
div = -1 - (left - 1) / right;
}
return PyLong_FromLong(div);
}
Do we need another fast-path in long_div(a, b) when b == _PyLong_One? Just
return a in this case.
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue41902>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com