我注意到一些奇怪的行为,可能是也可能不是我的系统特有的。(运行windows 8的联想t430)
使用此脚本:
import time
now = time.time()
while True:
then = now
now = time.time()
dif = now - then
print(dif)
time.sleep(0.01)
我在浏览器打开时得到以下输出(我认为是名义上的)。
但是,如果没有打开浏览器,我会观察到严重的每循环延迟
显然,这与直觉背道而驰,因为我认为当您拥有更少的concurrant进程时,任何人都会期望更好的性能
对这些结果的任何见解或简单复制都将不胜感激
编辑: 有趣的是,我观察到了与此代码类似的延迟:
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表示)-但我仍然注意到15毫秒的行为,其中打印计数将高达70k。。。以及10毫秒的行为,计数约为40k
我在windows和ubuntu服务器(virtualbox)(没有浏览器)上都尝试了同样的方法,但结果与我得到的平均结果相同
在Ubuntu服务器中
在窗户里
因此,在我看来,在开放的浏览器和python的性能之间没有相关性
给你:
使用您的代码和最新发布的Chrome,我可以用几乎相同的结果来确认这种行为
我测量了平均花费的时间-
正在运行的浏览器:0.01055538261329734
浏览器未运行:0.0156305539053695
我有大约30个打开的选项卡,但它们都是空闲的。目前,我想不出为什么会发生这种情况
期待进一步的见解
我额外启动了Windows7来复制你的发现,我可以证实这一点
这是一个Windows的东西,使用的定时器类型和默认分辨率为15.6毫秒(最小0.5毫秒)。应用程序可以改变当前分辨率(WinAPI函数:timeBeginPeriod),Chrome也可以这样做
2014年在Forbes杂志上发表的一篇文章涵盖了铬元素的bug,这将使分辨率永久设置为1ms,无论当前负载需要什么-这是一个问题,因为这是一个系统范围内的影响,会影响能耗。从该条中:
可以使用time.get_clock_info()从Python获得默认分辨率
您可以使用ClockRes小程序从
cmd
获取实际分辨率相关问题 更多 >
编程相关推荐