为什么time.sleep()的精度会受到Chrome的影响? [英] Why is time.sleep() accuracy influenced by Chrome?

查看:124
本文介绍了为什么time.sleep()的精度会受到Chrome的影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到某些奇怪的行为,可能特定于我的系统,也可能不特定于我的系统. (联想t430运行Windows 8)

I've noticed some strange behaviour that may or may not be specific to my system. (lenovo t430 running windows 8)

使用此脚本:

import time

now = time.time()
while True:
    then = now
    now = time.time()
    dif = now - then
    print(dif)
    time.sleep(0.01)

在打开浏览器的情况下,我得到以下输出(我认为是名义上的).

I get the following output (what I would consider nominal) with a browser open.

但是,在没有打开浏览器的情况下,我发现每个循环都有严重的延迟.

However without a browser open I observe a severe per loop latency.

很明显,这是违反直觉的,因为我认为当并发进程较少时,任何人都希望性能更好.

Obviously this is counter-intuitive as I think anyone would expect better performance when you have fewer concurrant processes.

对于这些结果的任何见解或简单复制,将不胜感激.

Any insights or simple replication of these results would be appreciated.

有趣的是,我在这段代码中观察到了类似的延迟:

Interestingly I observe similar latency with this code:

import time

now = time.time()

def newSleep(mark,duration):
    count = 0
    while time.time()-mark < duration:
        count+=1
    print(count)


while True:
    then = now
    now = time.time()
    dif = now - then
    print(dif)
    #time.sleep(0.01)
    newSleep(now,0.01)

虽然确实提供了更多的见解-某些潜在循环的实例是由于缺乏处理器可用性(注意到正在打印的计数为0)-我仍然注意到15ms的行为,其中打印的计数将高达70k ...和10ms的行为,计数约为40k.

While it does provide additional insight - that is some instances of latent loops are due to lack of processor availability (noted by a count of 0 being printed)- I still notice the 15ms behavior where the printed count will be as high as 70k... and 10ms behavior with counts around 40k.

推荐答案

我额外启动了Windows 7以复制您的发现,我可以确认.

I extra fired up Windows 7 to replicate your findings and I can confirm it.

这是 Windows ,其中使用了计时器类型默认分辨率为15.6毫秒(最低0.5毫秒).应用程序可以更改当前分辨率(WinAPI函数: timeBeginPeriod ),而Chrome会这样做.

It's a Windows thing with the type of timer used and a default resolution of 15.6 ms (minimum 0.5 ms). Applications can alter the current resolution (WinAPI function: timeBeginPeriod) and Chrome does so.

此功能会影响Windows的全局设置. Windows使用 任何过程要求的最低值(即最高分辨率). 设置更高的分辨率可以提高超时精度 等待功能的间隔.但是,它也可以减少总体 系统性能,因为线程调度程序可以更多地切换任务 经常.高分辨率也会阻止CPU电源管理 系统进入省电模式.设置更高的分辨率 不能提高高分辨率性能的准确性 柜台.

This function affects a global Windows setting. Windows uses the lowest value (that is, highest resolution) requested by any process. Setting a higher resolution can improve the accuracy of time-out intervals in wait functions. However, it can also reduce overall system performance, because the thread scheduler switches tasks more often. High resolutions can also prevent the CPU power management system from entering power-saving modes. Setting a higher resolution does not improve the accuracy of the high-resolution performance counter.

在Chrome中可以将分辨率永久设置为1毫秒(无论当前负载如何),这是一个问题,因为这是对能量消耗有影响的系统范围的影响.从那篇文章中:

An article from 2014 in Forbes is covering a bug in Chrome which would set the resolution permanently to 1 ms no matter what current load would require - a problem because it's a system-wide effect with impact on energy consumption. From that article:

在Windows之类的操作系统中,事件通常设置为每隔一定时间运行一次.到 省电,处理器在不需要注意的情况下进入睡眠状态,并且 以预定义的间隔唤醒.这是Chrome调整的时间间隔 Windows,因此将其减小到1.000ms意味着系统正在唤醒 比在15.625ms处更频繁.实际上,在1.000ms处处理器 每秒醒来1000次.默认值15.625ms表示 处理器每秒仅唤醒64次以检查所需的事件 注意.

In an OS like Windows, events are often set to run at intervals. To save power, the processor sleeps when nothing needs attention, and wakes at predefined intervals. This interval is what Chrome adjusts in Windows, so reducing it to 1.000ms means that the system is waking far more often than at 15.625ms. In fact, at 1.000ms the processor is waking 1000 times per second. The default, of 15.625ms means the processor wakes just 64 times per second to check on events that need attention.

Microsoft本身说1.000ms的滴答速度可能会增加功率 消费最多减少了25%.

Microsoft itself says that tick rates of 1.000ms might increase power consumption by "as much as 25 per cent".

您可以使用 time.get_clock_info()从Python获取默认分辨率. .

namespace = time.get_clock_info('time')
namespace.adjustable
# True
namespace.implementation
# 'GetSystemTimeAsFileTime()'
namespace.monotonic
# False
namespace.resolution
# 0.015600099999999999

您可以使用 ClockRes cmd获取实际分辨率. >小程序.

You can get the actual resolution from cmd with the ClockRes applet.

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

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