When using python datetime.now() I only get a resolution close to the millisecond. I need a microsecond resolution for network purposes, or at least being able to mesure the time elapsed between two received packets at a microsecond resolution.
Here is an example of the resolution I get:
from datetime import datetime
for i in range(10):
print(datetime.now().microsecond)
it returns :
224000
239000
247000
255000
264000
272000
284000
295000
308000
319000
解决方案
Use the right clock for your platform; use timeit.default_timer.
datetime uses the same C API used by time.time(), which on Windows doesn't offer as much granularity as time.clock() offers. timeit.default_timer points to the right version for your platform to offer the best choice.
在Python中使用datetime.now()获取时间时,默认精度只到毫秒。为了达到微秒级别的精度,特别是在网络包时间戳或测量微小时间间隔时,需要使用更精确的计时器。通常,timeit.default_timer()会提供更适合的高精度计时器,尤其在Windows系统上,它比time.time()和time.clock()更能提供细微的粒度。
1万+

被折叠的 条评论
为什么被折叠?



