为什么 time.sleep() 在 Windows 中这么慢? [英] Why time.sleep() is so slow in Windows?

查看:36
本文介绍了为什么 time.sleep() 在 Windows 中这么慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我循环了 1000 次,延时为 1 毫秒,并计算了总时间.很有趣的是,总时间是 15.6 秒而不是 1 秒.当我打开 Google Chrome 浏览一些网站时,它正确运行,总共 1 秒.此外,它在 Macbook 上也运行良好.我想知道我需要采取什么样的解决方案来解决这个问题?请尝试在不打开 Chrome 的情况下运行它,并在打开 Chrome 的情况下再次运行它以查看差异.当 Quora、Reddit 或 Stackoverflow 在我的系统上打开时,它运行正常.

I'm looping 1000 times with time delay 1ms and calculating total time. It's very interesting how the total time is 15.6 seconds instead of 1. When I opened Google Chrome and surfed some websites, it ran correctly with 1 sec total. Also, it ran fine with Macbook too. I'm wondering what kind of solutions that I need to do to fix this problem? Please try to run it without Chrome opened an again with Chrome opened to see the difference. It ran normally when Quora or Reddit or Stackoverflow opened on my system.

from timeit import default_timer as timer
import time
start = timer()
for i in range(1000):
    time.sleep(0.001)

end = timer()
print ("Total time: ", end - start)

我没有在 Python 上运行它.我刚刚打开Chrome浏览了一些网站,以加快时间延迟.

I didn't run it on Python. I just opened up Chrome and browsed some websites to speed up the time delay.

更新:关于 Windows 的计时器分辨率.所以基本上,Chrome 将计时器分辨率从 15.6ms 更改为 1ms.这篇文章解释得很好:https://randomascii.wordpress.com/2013/07/08/windows-timer-resolution-megawatts-wasted/

Updated: It's about the timer resolution from Windows. So basically, Chrome changed the timer resolution from 15.6ms to 1ms. This article explains very well: https://randomascii.wordpress.com/2013/07/08/windows-timer-resolution-megawatts-wasted/

推荐答案

我终于想通了.非常感谢您的评论.这些给了我解决它的提示.为了解释为什么会发生这种情况,Windows 操作系统将其默认计时器分辨率设置为 15.625 毫秒或 64 赫兹,这对于大多数应用程序来说已经足够了.但是,对于需要非常短的采样率或时间延迟的应用,15.625 ms 是不够的.因此,当我自己运行我的程序时,它停留在 15.6 秒的 1000 点.但是,当 Chrome 打开时,更高分辨率的计时器被触发并更改为 1 毫秒而不是 15.6,这导致我的程序按预期运行.

I finally figured it out. Thanks a lot for the comments. Those gave me hints to solve it. To explain why this happened, Windows OS has its default timer resolution set to 15.625 ms or 64 Hz, which is decently enough for most of the applications. However, for applications that need very short sampling rate or time delay, then 15.625 ms is not sufficient. Therefore, when I ran my program itself, it's stuck at 15.6 s for 1000 points. However, when Chrome is opened, the higher resolution timer is triggered and changed to 1 ms instead of 15.6, which caused my program to run as expected.

因此,为了解决它,我需要调用一个名为 timeBeginPeriod(period) 的 Windows 函数来更改分辨率计时器.幸运的是,python 通过提供 ctypes 库让我很容易修复它.最终代码如下:

Therefore, in order to solve it, I needed to call a Windows function named timeBeginPeriod(period) to change the resolution timer. Fortunately, python made it easy for me to fix it by providing ctypes library. The final code is provided below:

from time import perf_counter as timer
import time
from ctypes import windll #new

timeBeginPeriod = windll.winmm.timeBeginPeriod #new
timeBeginPeriod(1) #new

start = timer()
for i in range(1000):
    print (i)
    time.sleep(0.001)

end = timer()
print ("Total time: ", end - start)

警告:我阅读了有关这种高计时器分辨率将如何影响整体性能和电池的信息.我还没有看到任何事情发生,Windows 任务管理上活动的 CPU 使用率似乎也没有压倒一切.但如果您的应用程序碰巧导致一些奇怪的行为,请记住这一点.

Warning: I read about how this high timer resolution will affect the overall performance and also the battery. I have not seen anything happening yet, and CPU Usage on Activity on Windows Task Manage doesn't seem to overwhelming either. But keep that in mind if your applications happen to cause some strange behaviors.

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

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