Reputation: 123
I am using Python 3.7.6, with IPython 7.12.0
Code to replicate:
import datetime
mylist = []
for _ in range(10000):
mylist.append(datetime.datetime.now())
Looking at mylist
, I get chunks of about 2000 identical datetimes that are about 1ms apart. Because datetime.datetime objects have a resolution of 1µs, I should theoretically be getting chunks of 2 items that are 1µs apart instead.
What is happening here? Is the return value for datetime.datetime.now() cached for 1ms? If so, is this done on the Python side or the C side?
Upvotes: 1
Views: 939
Reputation: 42492
What is happening here? Is the return value for datetime.datetime.now() cached for 1ms? If so, is this done on the Python side or the C side?
There is no caching involved. As documented datetime.now
defers to time.time
which is not a high-precision clock and (also as documented) may not even have sub-second resolution:
Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second.
The resolution of datetime.now
will therefore depend on the system it's run on and that system's configuration.
Assuming you are running on windows, PEP 564 documented the resolution of time.time()
as 894us, or 0.9ms, close enough to 1ms (for Windows 8).
You can use the alternative clocks it documents if you need higher resolution:
time.time_ns()
has a resolution of ~300us on Windowsperf_counter()
has a resolution of 100ns however it does not return a timestamp, so there's more work involved as you'll need to save a reference timestamp and perf_counter then use these in order to offset following perf counters and get actual timestamps.Upvotes: 3