Python中的微妙微秒分辨率 [英] Bad microsecond resolution in Python

查看:309
本文介绍了Python中的微妙微秒分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用python datetime.now()我只得到一个接近毫秒的分辨率。我需要一个微秒的分辨率用于网络目的,或者至少能够以微秒分辨率来测量两个接收的数据包之间的时间。

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)    

它返回:

224000
239000
247000
255000
264000
272000
284000
295000
308000
319000


推荐答案

您的平台的正确时钟;使用 timeit.default_timer

Use the right clock for your platform; use timeit.default_timer.

datetime 使用与 time.time相同的C API (),它在Windows上不提供与 time.clock()提供的细粒度。 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中的微妙微秒分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆