diff options
author | Shantanu <12621235+hauntsaninja@users.noreply.github.com> | 2023-10-06 22:57:18 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-06 22:57:18 (GMT) |
commit | f013b475047b2e9d377feda9f2e16e5cdef824d7 (patch) | |
tree | 5d3211d99197307353471c3cf5e700b6bef3e612 | |
parent | 201dc11aeb4699de3c5ebaea9798796c30087bcc (diff) | |
download | cpython-f013b475047b2e9d377feda9f2e16e5cdef824d7.zip cpython-f013b475047b2e9d377feda9f2e16e5cdef824d7.tar.gz cpython-f013b475047b2e9d377feda9f2e16e5cdef824d7.tar.bz2 |
gh-110489: Optimise math.ceil for known exact float (#108801)
This matches a similar optimisation done for math.floor in
https://github.com/python/cpython/pull/21072
-rw-r--r-- | Misc/NEWS.d/next/Core and Builtins/2023-10-06-22-30-25.gh-issue-110489.rI2n8A.rst | 1 | ||||
-rw-r--r-- | Modules/mathmodule.c | 16 |
2 files changed, 10 insertions, 7 deletions
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-10-06-22-30-25.gh-issue-110489.rI2n8A.rst b/Misc/NEWS.d/next/Core and Builtins/2023-10-06-22-30-25.gh-issue-110489.rI2n8A.rst new file mode 100644 index 0000000..6252d00 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-10-06-22-30-25.gh-issue-110489.rI2n8A.rst @@ -0,0 +1 @@ +Optimise :func:`math.ceil` when the input is exactly a float, resulting in about a 10% improvement. diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 2d896e7..a4d9466 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -1125,8 +1125,12 @@ static PyObject * math_ceil(PyObject *module, PyObject *number) /*[clinic end generated code: output=6c3b8a78bc201c67 input=2725352806399cab]*/ { + double x; - if (!PyFloat_CheckExact(number)) { + if (PyFloat_CheckExact(number)) { + x = PyFloat_AS_DOUBLE(number); + } + else { math_module_state *state = get_math_module_state(module); PyObject *method = _PyObject_LookupSpecial(number, state->str___ceil__); if (method != NULL) { @@ -1136,11 +1140,10 @@ math_ceil(PyObject *module, PyObject *number) } if (PyErr_Occurred()) return NULL; + x = PyFloat_AsDouble(number); + if (x == -1.0 && PyErr_Occurred()) + return NULL; } - double x = PyFloat_AsDouble(number); - if (x == -1.0 && PyErr_Occurred()) - return NULL; - return PyLong_FromDouble(ceil(x)); } @@ -1196,8 +1199,7 @@ math_floor(PyObject *module, PyObject *number) if (PyFloat_CheckExact(number)) { x = PyFloat_AS_DOUBLE(number); } - else - { + else { math_module_state *state = get_math_module_state(module); PyObject *method = _PyObject_LookupSpecial(number, state->str___floor__); if (method != NULL) { |