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.
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.
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.
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.
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.
Yes, time. sleep will halt your program.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With