Wilbert's website at SocSci

has a subtitle

computer/sleepAccuracy.html 2015-06-11

Computer sleep accuracy

Following graph shows the requested and the actual sleep time of the Python sleep function on my computer. The actual time was measured with time.time for Linux and with time.clock on Windows.

Randomizing the starting times of the sleeps alters the result for Windows 7 considerably (next plot). Apparently the sleeps always end on an integer millisecond interrupt.

Following graph shows the requested sleep time and the absolute error on the actual sleep time of the Python sleep function on my computer. For longer sleep periods the 1‰ excess continues.

The last graph shows something similar for Linux zoomed in.

This page contains a lot of wisdom on this sobject.

The following function (and slight variations for output of the raw data) was used for making these graphs:

def sleepAccuracyTest(n=20, timeFunction=time.time, minTime=0.01, maxTime=0.1, unSync=False):
        dtErrorList = []
        for i in range(n):
                if unSync:
                        size = np.random.randint(200, 220)
                        a = np.random.rand(size, size)
                        a  =a *a
                        sys.stderr.write(str(a[10][10]))
                dtRequest = minTime+np.random.rand()*(maxTime-minTime)
                t0 = timeFunction()
                time.sleep(dtRequest)
                dtError = timeFunction()-t0-dtRequest
                dtErrorList.append(dtError)
                #print("{}\t{}".format(dtRequest, dtError)) 
        return np.std(dtErrorList, ddof=1)