python - 替代 time.sleep

标签 python performance sleep busy-waiting

简介:众所周知,time.sleep 的准确性取决于操作系统和计算负载。 Windows 中的准确性非常差。

类似于/questions/17499837一个方法可以使用 time.clock 方法作为 time.sleep 的替代方法来实现忙等待。这种方法会产生不必要的负载,影响系统中的其他模块。这在进行模拟时是不可取的。

为了减少忙等待和不依赖于 time.sleep 的时间,一个类使用方法 select.select 并利用超时属性。见下面的代码:

from sys import platform as _platform
import time, select, socket

class HighResolutionTimeStamp():
    __init = time.clock()
    __base = time.time()

    def __init__(self):
        self.__fd = socket.socket()
        self.dtts = time.clock if _platform == 'win32' else time.time

    def __del__(self):
        self.__fd.close()

    def get_high_resolution_dt(self):
        return HighResolutionTimeStamp.__base + self.dtts() if _platform == 'win32' else time.time()

    def busy_wait(self, wait_time):
        currentTime = self.dtts()
        while (self.dtts() <= currentTime + wait_time):
            pass

    def sleep(self, wait_time):
        currentTime = self.dtts()
        while (self.dtts() < (currentTime + wait_time - 0.001)):
            select.select([self.__fd], [], [], 0.001)
        while (self.dtts() < currentTime + wait_time):
            select.select([self.__fd], [], [], 0.0)

if __name__ == '__main__':
    st = 1.0/80.0
    it = 10
    ts = 1

    time.sleep(ts)
    hrdr = HighResolutionTimeStamp()
    total = hrdr.get_high_resolution_dt()
    for i in range(it):
        hrdr.busy_wait(st)
    print 'Ellapsed:', hrdr.get_high_resolution_dt() - total

    time.sleep(ts)
    total = hrdr.get_high_resolution_dt()
    for i in range(it):
        hrdr.sleep(st)
    print 'Ellapsed:', hrdr.get_high_resolution_dt() - total

    time.sleep(ts)
    total = hrdr.get_high_resolution_dt()
    for i in range(it):
        time.sleep(st)
    print 'Ellapsed:', hrdr.get_high_resolution_dt() - total

环境:我正在使用 PortablePython2.7.6.1

问题:当代码在 PyScripter 或在后台打开 PyScripter 的命令行中执行时,上面的脚本执行非常准确。一旦 PyScripter 关闭,方法 sleep 就会变得不准确。我知道 select.select 的超时应该像 time.sleep 一样不准确,但在所有情况下,都不是如上所述。

结果:

没有 PyScripter 在后台运行

C:\..\PortablePython2.7.6.1\App\python.exe highresolutiondt.py

Busy wait. Ellapsed: 0.125249385834

Sleep. Ellapsed: 0.15624165535

Time.sleep. Ellapsed: 0.156844139099

在后台运行 PyScripter

C:\..\PortablePython2.7.6.1\App\python.exe highresolutiondt.py

Busy wait. Ellapsed: 0.125702142715

Sleep. Ellapsed: 0.125874519348

Time.sleep. Ellapsed: 0.120799064636

最佳答案

这使用自 unix 纪元以来的时间,我很确定它更准确,不过我不使用 Windows,所以我没有测试它。

from time import time

def pause(secs):
    init_time = time()
    while time() < init_time+secs: pass

print("See ya in 10 seconds")
pause(10)
print("Heeeeeelooooo there")

希望对你有帮助

关于python - 替代 time.sleep,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27618949/

相关文章:

python - 在 Open3D 中不打开 Visualizer 捕获深度图像?

c - 如何在 C 中使用 putpixel() 时获得高帧率?

MySQL - 提高慢速子查询的性能

php - php上的休眠功能

c - 使用 sleep 和 select 信号

java - 为什么 PrintWriter 不能与 Thread.sleep() 一起使用

python - django 比较 datetime.now 和 pub_date

python - 将长表转换为宽表并根据行创建列

python - 使用 issubset 和空集时如何返回 False

python - 如何检查数据框中的所有值是否均为 True