Vaibhav Bajaj
Vaibhav Bajaj

Reputation: 1934

datetime.fromtimestamp() takes longer than datetime.utcfromtimestamp()

I have written a statistics / ML program that performs some data analysis on Time Series data. Surprisingly, when I get timestamps using the fromtimestamp() method, my program executes in roughly 15 seconds, whereas when using the utcfromtimestamp(), it executes in a second or less.

To test and compare their runtimes, I ran a python program:

timestamp = datetime.now().timestamp()

start = datetime.now()
date1 = datetime.fromtimestamp(timestamp)
print(datetime.now() - start)

start = datetime.now()
date2 = datetime.utcfromtimestamp(timestamp)
print(datetime.now() - start)

But I found fromtimestamp() to be consistently faster in this case.

Runtime of fromtimestamp - 00.000004
Approx Runtime of utcfromtimestamp - 0.000267

In my program though, fromtimestamp() takes roughly 0.4 seconds.

Are there some hidden factors at play here? Note, in my ML program I do threading, and multiprocessing. I'm not sure if that may be the factor at play here.

Upvotes: 2

Views: 1115

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122282

The hidden factor is that datetime.fromtimestamp() produces local time. From the documentation:

Return the local date and time corresponding to the POSIX timestamp

while datetime.utcfromtimestamp() returns UTC time:

Return the UTC datetime corresponding to the POSIX timestamp

A POSIX timestamp represents time in UTC, always. So it is much simpler to convert that number into a UTC datetime object (no timezone conversion required) versus having to look up the system timezone and then applying the timezone adjustments required.

The difference between the functions basically comes down to using time.localtime() vs time.gmtime(), which are covered by the localtime_r / gmtime_r functions (on POSIX systems) or the localtime_s / gmtime_s functions (on Windows).

Your timing test is otherwise badly flawed. Use the timeit module to run time trials:

>>> from datetime import datetime
>>> import timeit
>>> timestamp = datetime.now().timestamp()
>>> timeit.timeit("datetime.fromtimestamp(timestamp)", "from __main__ import datetime, timestamp")
0.6479804780101404
>>> timeit.timeit("datetime.utcfromtimestamp(timestamp)", "from __main__ import datetime, timestamp")
0.2666302509896923

This shows datetime.utcfromtimestamp() to be faster, consistently.

Upvotes: 2

Related Questions