Why is time.sleep() accuracy influenced by Chrome?
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.
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
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.