You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
datetime: Speed up timedelta arithmetic by using int64_t directly #162467
For a UTC/aware datetime, datetime_timestamp() subtracts the UNIX epoch and calls total_seconds() on the resulting timedelta. To get the total number of microseconds, delta_to_microseconds() does 2 multiplications and 2 additions on Python integers.
However, days * 86400 + seconds fits in int64_t for every timedelta, so it can be computed in C. (The final multiplication by 10**6 can overflow int64_t for very large deltas, so there'd be still a need for PyLong fallback.)
Benchmark (PGO+LTO, macOS arm64) results:
Operation
Before
After
Speedup
timestamp() on a UTC datetime
95.0 ns
44.2 ns
2.13x
d.total_seconds() (3 days plus fractional seconds)
changed the title [-]`datetime`: Speed up `timedelta.total_seconds()` by using `int64_t` directly[/-][+]`datetime`: Speed up `timedelta` arithmetic by using `int64_t` directly[/+]on Nov 23, 2026
Activity
[-]`datetime`: Speed up `timedelta.total_seconds()` by using `int64_t` directly[/-][+]`datetime`: Speed up `timedelta` arithmetic by using `int64_t` directly[/+]