[英]Python resolution of datetime.now()
from datetime import datetime
import time
for i in range(1000):
curr_time = datetime.now()
print(curr_time)
time.sleep(0.0001)
I was testing the resolution of datetime.now()
. 我正在测试datetime.now()
的分辨率。 Since it supposes to output in microsecond, I expected that each print will be different. 由于它假定以微秒为单位输出,因此我希望每张纸都会有所不同。
However, I always get something like that. 但是,我总是得到类似的东西。
...
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.212073
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
2015-07-10 22:38:47.213074
...
Why does that happen? 为什么会这样呢? Is there any way that I can get an accurate timestamp down to the microsecond? 有什么方法可以使精确的时间戳降低到微秒? Actually I don't need microseconds, but it would be nice to get 0.1ms resolution. 实际上,我不需要微秒,但获得0.1ms分辨率会很好。
=== UPDATE ==== ===更新====
I compared it with using time.perf_counter() and adding to the starting datetime
from datetime import datetime, timedelta import time 我将其与使用time.perf_counter()进行比较,并从datetime导入datetime,timedelta导入时间添加到起始datetime
datetime0 = datetime.now()
t0 = time.perf_counter()
for i in range(1000):
print('datetime.now(): ', datetime.now())
print('time.perf_counter(): ', datetime0 + timedelta(0, time.perf_counter()-t0))
print('\n')
time.sleep(0.000001)
I am not sure how 'accurate' it really is, but the resolution is at least higher.... it doesn't seems to matter as my computer cannot even print at a speed that high. 我不确定它到底有多精确,但是分辨率至少更高。...这似乎无关紧要,因为我的计算机甚至无法以如此高的速度打印。 For my purpose, which I simply need different timestamps to distinguish different entries, this is good enough for me. 就我而言,我只需要不同的时间戳来区分不同的条目,这对我来说已经足够了。
...
datetime.now(): 2015-07-10 23:24:18.010377
time.perf_counter(): 2015-07-10 23:24:18.010352
datetime.now(): 2015-07-10 23:24:18.010377
time.perf_counter(): 2015-07-10 23:24:18.010545
datetime.now(): 2015-07-10 23:24:18.010377
time.perf_counter(): 2015-07-10 23:24:18.010745
datetime.now(): 2015-07-10 23:24:18.011377
time.perf_counter(): 2015-07-10 23:24:18.010961
datetime.now(): 2015-07-10 23:24:18.011377
time.perf_counter(): 2015-07-10 23:24:18.011155
datetime.now(): 2015-07-10 23:24:18.011377
time.perf_counter(): 2015-07-10 23:24:18.011369
datetime.now(): 2015-07-10 23:24:18.011377
time.perf_counter(): 2015-07-10 23:24:18.011596
datetime.now(): 2015-07-10 23:24:18.012379
time.perf_counter(): 2015-07-10 23:24:18.011829
datetime.now(): 2015-07-10 23:24:18.012379
time.perf_counter(): 2015-07-10 23:24:18.012026
datetime.now(): 2015-07-10 23:24:18.012379
time.perf_counter(): 2015-07-10 23:24:18.012232
datetime.now(): 2015-07-10 23:24:18.012379
time.perf_counter(): 2015-07-10 23:24:18.012424
datetime.now(): 2015-07-10 23:24:18.012379
time.perf_counter(): 2015-07-10 23:24:18.012619
datetime.now(): 2015-07-10 23:24:18.013380
time.perf_counter(): 2015-07-10 23:24:18.012844
datetime.now(): 2015-07-10 23:24:18.013380
time.perf_counter(): 2015-07-10 23:24:18.013044
datetime.now(): 2015-07-10 23:24:18.013380
time.perf_counter(): 2015-07-10 23:24:18.013242
datetime.now(): 2015-07-10 23:24:18.013380
time.perf_counter(): 2015-07-10 23:24:18.013437
datetime.now(): 2015-07-10 23:24:18.013380
time.perf_counter(): 2015-07-10 23:24:18.013638
datetime.now(): 2015-07-10 23:24:18.014379
time.perf_counter(): 2015-07-10 23:24:18.013903
datetime.now(): 2015-07-10 23:24:18.014379
time.perf_counter(): 2015-07-10 23:24:18.014125
datetime.now(): 2015-07-10 23:24:18.014379
time.perf_counter(): 2015-07-10 23:24:18.014328
datetime.now(): 2015-07-10 23:24:18.014379
time.perf_counter(): 2015-07-10 23:24:18.014526
datetime.now(): 2015-07-10 23:24:18.014379
time.perf_counter(): 2015-07-10 23:24:18.014721
datetime.now(): 2015-07-10 23:24:18.015381
time.perf_counter(): 2015-07-10 23:24:18.014919
...
This may be a limitation of time.sleep
on your system, rather than datetime.now()
... or possibly both. 这可能是系统上time.sleep
的限制,而不是datetime.now()
...或两者都有。 What OS and what version and distribution of Python are you running on? 您正在运行什么操作系统以及什么版本和Python发行版?
Your system may not offer the "subsecond precision" mentioned in the time.sleep
docs: 您的系统可能不提供time.sleep
文档中提到的“亚秒精度”:
sleep(...)
sleep(seconds)
Delay execution for a given number of seconds. The argument may be
a floating point number for subsecond precision.
On Linux 3.x on amd64 with CPython 2.7 , I get something pretty close to the 0.0001 time steps that you intended: 在具有CPython 2.7的amd64上的Linux 3.x上 ,我得到的结果非常接近您想要的0.0001时间步长:
2015-07-10 19:58:24.353711
2015-07-10 19:58:24.353879
2015-07-10 19:58:24.354052
2015-07-10 19:58:24.354227
2015-07-10 19:58:24.354401
2015-07-10 19:58:24.354577
2015-07-10 19:58:24.354757
2015-07-10 19:58:24.354938
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.