windows - Python resolution of datetime.now() -
from datetime import datetime import time in range(1000): curr_time = datetime.now() print(curr_time) time.sleep(0.0001)
i testing resolution of datetime.now()
. since supposes output in microsecond, expected each print different.
however, 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 happen? there way can accurate timestamp down microsecond? don't need microseconds, nice 0.1ms resolution.
=== update ====
i compared using time.perf_counter() , adding starting datetime
datetime import datetime, timedelta import time
datetime0 = datetime.now() t0 = time.perf_counter() 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 not sure how 'accurate' is, resolution @ least higher.... doesn't seems matter computer cannot print @ speed high. purpose, need different timestamps distinguish different entries, enough 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 limitation of time.sleep
on system, rather datetime.now()
... or possibly both. what os , version , distribution of python running on?
your system may not offer "subsecond precision" mentioned in time.sleep
docs:
sleep(...) sleep(seconds) delay execution given number of seconds. argument may floating point number subsecond precision.
on linux 3.x on amd64 cpython 2.7, pretty close 0.0001 time steps intended:
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
Comments
Post a Comment