Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is time.sleep() accuracy influenced by Chrome?

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

With this script:

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.

enter image description here

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

enter image description here

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.

EDIT: 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)

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.

like image 843
kpie Avatar asked Jan 16 '20 17:01

kpie


People also ask

Is python time sleep accurate?

Answer #1: The accuracy of the time. sleep function depends on 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.

What is the argument for sleep method in time module?

Description. Python time method sleep() suspends execution for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time.

Does time sleep stop all code?

Yes, time. sleep will halt your program.

How does Python sleep work?

The sleep() function in python's time module is used to suspend the execution of a program for the given number of seconds. This means that the program is paused for the given time and after that time has passed, the program gets executed automatically.


3 Answers

I tried the same within both windows and ubuntu server(virtualbox)(which doen't have a browser) but the result's are same which average i am getting

in Ubuntu Server

    0.010122537612915039     0.010426998138427734     0.010067939758300781     0.010767221450805664     0.010728120803833008     0.010106086730957031     0.01068258285522461     0.010105609893798828     0.01118612289428711     0.010136842727661133     0.010585784912109375     0.010425567626953125     0.01014852523803711     0.010422945022583008     0.01010894775390625 

and in Windows

    0.010767221450805664     0.010751485824584961     0.010716915130615234     0.010229110717773438     0.01016545295715332     0.010195255279541016     0.010723352432250977     0.010744094848632812     0.010716438293457031     0.010564565658569336     0.010889291763305664     0.010728597640991211     0.010579824447631836     0.010889530181884766     0.010567903518676758     0.010717153549194336     0.010735273361206055 

so, in my opinion, there is no correlation between the opened browser and performance of python

like image 30
i_am_deesh Avatar answered Sep 18 '22 12:09

i_am_deesh


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

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.

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.

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:

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 itself says that tick rates of 1.000ms might increase power consumption by "as much as 25 per cent".

You can get the default resolution from Python with time.get_clock_info().

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

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

like image 189
Darkonaut Avatar answered Sep 19 '22 12:09

Darkonaut


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

Here you go:

Using your code and the most recent release of Chrome, I can confirm this behaviour with nearly the same results.

I measured the average time taken-

Browser running: 0.01055538261329734

Browser not running: 0.01563055389053695

I have about 30 open tabs, but they are all idle. Currently, I can't think of any reason why this would happen.

Looking forward to further insights.

like image 23
DerHamm Avatar answered Sep 21 '22 12:09

DerHamm