How accurate is python's time.sleep()?

后端 未结 11 1755
有刺的猬
有刺的猬 2020-11-22 10:41

I can give it floating point numbers, such as

time.sleep(0.5)

but how accurate is it? If i give it

time.sleep(0.05)
         


        
相关标签:
11条回答
  • 2020-11-22 11:04

    You can't really guarantee anything about sleep(), except that it will at least make a best effort to sleep as long as you told it (signals can kill your sleep before the time is up, and lots more things can make it run long).

    For sure the minimum you can get on a standard desktop operating system is going to be around 16ms (timer granularity plus time to context switch), but chances are that the % deviation from the provided argument is going to be significant when you're trying to sleep for 10s of milliseconds.

    Signals, other threads holding the GIL, kernel scheduling fun, processor speed stepping, etc. can all play havoc with the duration your thread/process actually sleeps.

    0 讨论(0)
  • 2020-11-22 11:04

    Tested this recently on Python 3.7 on Windows 10. Precision was around 1ms.

    0 讨论(0)
  • 2020-11-22 11:05

    A small correction, several people mention that sleep can be ended early by a signal. In the 3.6 docs it says,

    Changed in version 3.5: The function now sleeps at least secs even if the sleep is interrupted by a signal, except if the signal handler raises an exception (see PEP 475 for the rationale).

    0 讨论(0)
  • 2020-11-22 11:06

    From the documentation:

    On the other hand, the precision of time() and sleep() is better than their Unix equivalents: times are expressed as floating point numbers, time() returns the most accurate time available (using Unix gettimeofday where available), and sleep() will accept a time with a nonzero fraction (Unix select is used to implement this, where available).

    And more specifically w.r.t. sleep():

    Suspend execution for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.

    0 讨论(0)
  • 2020-11-22 11:07

    if you need more precision or lower sleep times, consider making your own:

    import time
    
    def sleep(duration, get_now=time.perf_counter):
        now = get_now()
        end = now + duration
        while now < end:
            now = get_now()
    
    0 讨论(0)
  • 2020-11-22 11:08

    Why don't you find out:

    from datetime import datetime
    import time
    
    def check_sleep(amount):
        start = datetime.now()
        time.sleep(amount)
        end = datetime.now()
        delta = end-start
        return delta.seconds + delta.microseconds/1000000.
    
    error = sum(abs(check_sleep(0.050)-0.050) for i in xrange(100))*10
    print "Average error is %0.2fms" % error
    

    For the record, I get around 0.1ms error on my HTPC and 2ms on my laptop, both linux machines.

    0 讨论(0)
提交回复
热议问题