gh-110489: Optimise math.ceil for known exact float#108801
gh-110489: Optimise math.ceil for known exact float#108801hauntsaninja merged 3 commits intopython:mainfrom
Conversation
| if (PyErr_Occurred()) | ||
| return NULL; | ||
| x = PyFloat_AsDouble(number); | ||
| if (x == -1.0 && PyErr_Occurred()) |
There was a problem hiding this comment.
This will be partially covered by tests.
Modules/mathmodule.c
Outdated
| if (PyFloat_CheckExact(number)) { | ||
| x = PyFloat_AS_DOUBLE(number); | ||
| } | ||
| else |
There was a problem hiding this comment.
Maybe we should follow to the PEP 7 here?
This matches a similar optimisation done for math.floor in python#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 ```
3166603 to
7463f3a
Compare
|
Thanks for the review! I'd avoided PEP 7 for consistency with floor, but I just changed both |
|
|
) This matches a similar optimisation done for math.floor in python#21072
This matches a similar optimisation done by rhettinger for math.floor in #21072
Before:
After: