From 7463f3a409ce838ad2f5080237908c4ba6f88f83 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Fri, 1 Sep 2023 22:34:15 -0700 Subject: [PATCH 1/3] Optimise math.ceil for known exact float MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This matches a similar optimisation done for math.floor in https://github.com/python/cpython/pull/21072 Before: ``` λ ./python.exe -m timeit -r 11 -s 'from math import ceil' -s 'x=3.14' 'ceil(x)' 20000000 loops, best of 11: 13.3 nsec per loop λ ./python.exe -m timeit -r 11 -s 'from math import ceil' -s 'x=0.0' 'ceil(x)' 20000000 loops, best of 11: 13.3 nsec per loop λ ./python.exe -m timeit -r 11 -s 'from math import ceil' -s 'x=-3.14E32' 'ceil(x)' 10000000 loops, best of 11: 35.3 nsec per loop λ ./python.exe -m timeit -r 11 -s 'from math import ceil' -s 'x=-323452345.14' 'ceil(x)' 10000000 loops, best of 11: 21.8 nsec per loop ``` After: ``` λ ./python.exe -m timeit -r 11 -s 'from math import ceil' -s 'x=3.14' 'ceil(x)' 20000000 loops, best of 11: 11.8 nsec per loop λ ./python.exe -m timeit -r 11 -s 'from math import ceil' -s 'x=0.0' 'ceil(x)' 20000000 loops, best of 11: 11.7 nsec per loop λ ./python.exe -m timeit -r 11 -s 'from math import ceil' -s 'x=-3.14E32' 'ceil(x)' 10000000 loops, best of 11: 32.7 nsec per loop λ ./python.exe -m timeit -r 11 -s 'from math import ceil' -s 'x=-323452345.14' 'ceil(x)' 10000000 loops, best of 11: 20.1 nsec per loop ``` --- Modules/mathmodule.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 2d896e7fe333a4..2f29735df011e7 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -1125,8 +1125,13 @@ 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 +1141,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)); } From 5c46412e2f18944f035c01ced7656231aa6be6a8 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Fri, 6 Oct 2023 15:27:08 -0700 Subject: [PATCH 2/3] pep 7 --- Modules/mathmodule.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 2f29735df011e7..a4d94665592351 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -1130,8 +1130,7 @@ math_ceil(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___ceil__); if (method != NULL) { @@ -1200,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) { From 6c28b764a947750b522ee0d25540a19f50abc8da Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 6 Oct 2023 22:30:29 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023-10-06-22-30-25.gh-issue-110489.rI2n8A.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-10-06-22-30-25.gh-issue-110489.rI2n8A.rst 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 00000000000000..6252d00cc583d3 --- /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.