python的time.sleep()有多准确? [英] How accurate is python's time.sleep()?

查看:680
本文介绍了python的time.sleep()有多准确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以给它浮点数,例如

I can give it floating point numbers, such as

time.sleep(0.5)

但它的准确度是多少?如果我给它

but how accurate is it? If i give it

time.sleep(0.05)

是否真的睡了约50毫秒?

will it really sleep about 50 ms?

推荐答案

time.sleep功能取决于底层操作系统的睡眠准确性的准确性。对于非实时操作系统,如股票Windows,您可以睡觉的最小间隔约为10-13ms。在最短的10-13ms之内的几毫秒内,我已经看到了准确的睡眠。

The accuracy of the time.sleep function depends on the accuracy of your underlying OS's sleep accuracy. For non-realtime OS's like a stock Windows the smallest interval you can sleep for is about 10-13ms. I have seen accurate sleeps within several milliseconds of that time when above the minimum 10-13ms.

更新:
像下面列出的文档一样,这是常见的做一个循环的睡眠,如果早点唤醒你,确保回去睡觉。

Update: Like mentioned in the docs sited below, it's common to do the sleep in a loop that will make sure to go back to sleep if the wakes you up the early.

我还应该提到,如果你运行Ubuntu你可以通过安装rt内核软件包(至少在Ubuntu 10.04 LTS中)来尝试一个伪实时内核(使用RT_PREEMPT补丁集)。

I should also mention that if you are running Ubuntu you can try out a pseudo real-time kernel (with the RT_PREEMPT patch set) by installing the rt kernel package (at least in Ubuntu 10.04 LTS).

编辑: - 即时Linux内核的睡眠间隔最小,接近1ms,然后10ms,但以非确定性方式变化。

Correction non-realtime Linux kernels have minimum sleep interval much closer to 1ms then 10ms but it varies in a non-deterministic manner.

这篇关于python的time.sleep()有多准确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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