From 97a5690f558805004d981b64dce38ba27c41adf5 Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Thu, 23 Feb 2023 05:36:50 +0000 Subject: [PATCH] timestamp: Switch to a slightly faster datetime_to_timestamp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is quite a bit faster: ``` %timeit calendar.timegm(now.timetuple()) 2.91 µs ± 361 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each) %timeit int(now.timestamp()) 539 ns ± 27 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each) ``` This is particularly important for the presence endpoint, which is a tight loop of serializing datetimes. --- zerver/lib/timestamp.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/zerver/lib/timestamp.py b/zerver/lib/timestamp.py index f636d9a553..9098fcf6f9 100644 --- a/zerver/lib/timestamp.py +++ b/zerver/lib/timestamp.py @@ -1,4 +1,3 @@ -import calendar import datetime @@ -47,4 +46,4 @@ def timestamp_to_datetime(timestamp: float) -> datetime.datetime: def datetime_to_timestamp(dt: datetime.datetime) -> int: verify_UTC(dt) - return calendar.timegm(dt.timetuple()) + return int(dt.timestamp())