python延时函数 微秒,Python中的微妙微秒分辨率

在Python中使用datetime.now()获取时间时,默认精度只到毫秒。为了达到微秒级别的精度,特别是在网络包时间戳或测量微小时间间隔时,需要使用更精确的计时器。通常,timeit.default_timer()会提供更适合的高精度计时器,尤其在Windows系统上,它比time.time()和time.clock()更能提供细微的粒度。
摘要由CSDN通过智能技术生成

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值