classification
Title: Improve perfomance of PyLong_FromDouble()
Type: performance Stage:
Components: Interpreter Core Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, sir-sigurd
Priority: normal Keywords:

Created on 2019-08-30 09:00 by sir-sigurd, last changed 2019-08-30 14:29 by serhiy.storchaka.

Files
File name Uploaded Description Edit
bench-long-from-float.py sir-sigurd, 2019-08-30 09:00
Pull Requests
URL Status Linked Edit
PR 15611 open sir-sigurd, 2019-08-30 09:00
Messages (1)
msg350861 - (view) Author: Sergey Fedoseev (sir-sigurd) * Date: 2019-08-30 09:00
This patch simplifies fast path for floats that fit into C long and moves it from float.__trunc__ to PyLong_FromDouble().

+---------------------+---------------------+------------------------------+
| Benchmark           | long-from-float-ref | long-from-float              |
+=====================+=====================+==============================+
| int(1.)             | 39.5 ns             | 37.3 ns: 1.06x faster (-6%)  |
+---------------------+---------------------+------------------------------+
| int(2.**20)         | 46.4 ns             | 45.6 ns: 1.02x faster (-2%)  |
+---------------------+---------------------+------------------------------+
| int(2.**30)         | 52.5 ns             | 49.0 ns: 1.07x faster (-7%)  |
+---------------------+---------------------+------------------------------+
| int(2.**60)         | 50.0 ns             | 49.2 ns: 1.02x faster (-2%)  |
+---------------------+---------------------+------------------------------+
| int(-2.**63)        | 76.6 ns             | 48.6 ns: 1.58x faster (-37%) |
+---------------------+---------------------+------------------------------+
| int(2.**80)         | 77.1 ns             | 72.5 ns: 1.06x faster (-6%)  |
+---------------------+---------------------+------------------------------+
| int(2.**120)        | 91.5 ns             | 87.7 ns: 1.04x faster (-4%)  |
+---------------------+---------------------+------------------------------+
| math.ceil(1.)       | 57.4 ns             | 32.9 ns: 1.74x faster (-43%) |
+---------------------+---------------------+------------------------------+
| math.ceil(2.**20)   | 60.5 ns             | 41.3 ns: 1.47x faster (-32%) |
+---------------------+---------------------+------------------------------+
| math.ceil(2.**30)   | 64.2 ns             | 43.9 ns: 1.46x faster (-32%) |
+---------------------+---------------------+------------------------------+
| math.ceil(2.**60)   | 66.3 ns             | 42.3 ns: 1.57x faster (-36%) |
+---------------------+---------------------+------------------------------+
| math.ceil(-2.**63)  | 67.7 ns             | 43.1 ns: 1.57x faster (-36%) |
+---------------------+---------------------+------------------------------+
| math.ceil(2.**80)   | 66.6 ns             | 65.6 ns: 1.01x faster (-1%)  |
+---------------------+---------------------+------------------------------+
| math.ceil(2.**120)  | 79.9 ns             | 80.5 ns: 1.01x slower (+1%)  |
+---------------------+---------------------+------------------------------+
| math.floor(1.)      | 58.4 ns             | 31.2 ns: 1.87x faster (-47%) |
+---------------------+---------------------+------------------------------+
| math.floor(2.**20)  | 61.0 ns             | 39.6 ns: 1.54x faster (-35%) |
+---------------------+---------------------+------------------------------+
| math.floor(2.**30)  | 64.2 ns             | 43.9 ns: 1.46x faster (-32%) |
+---------------------+---------------------+------------------------------+
| math.floor(2.**60)  | 62.1 ns             | 40.1 ns: 1.55x faster (-35%) |
+---------------------+---------------------+------------------------------+
| math.floor(-2.**63) | 64.1 ns             | 39.9 ns: 1.61x faster (-38%) |
+---------------------+---------------------+------------------------------+
| math.floor(2.**80)  | 62.2 ns             | 62.7 ns: 1.01x slower (+1%)  |
+---------------------+---------------------+------------------------------+
| math.floor(2.**120) | 77.0 ns             | 77.8 ns: 1.01x slower (+1%)  |
+---------------------+---------------------+------------------------------+

I'm going to speed-up conversion of larger floats in a follow-up PR.
History
Date User Action Args
2019-08-30 14:29:21serhiy.storchakasetnosy: + mark.dickinson
2019-08-30 09:00:41sir-sigurdcreate