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

后端 未结 11 1767
有刺的猬
有刺的猬 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:12
    def test():
        then = time.time()  # get time at the moment
        x = 0
        while time.time() <= then+1:  # stop looping after 1 second
            x += 1
            time.sleep(0.001)  # sleep for 1 ms
        print(x)
    

    On windows 7 / Python 3.8 returned 1000 for me, even if i set the sleep value to 0.0005

    so a perfect 1ms

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

    People are quite right about the differences between operating systems and kernels, but I do not see any granularity in Ubuntu and I see a 1 ms granularity in MS7. Suggesting a different implementation of time.sleep, not just a different tick rate. Closer inspection suggests a 1μs granularity in Ubuntu by the way, but that is due to the time.time function that I use for measuring the accuracy. Linux and Windows typical time.sleep behaviour in Python

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

    Here's my follow-up to Wilbert's answer: the same for Mac OS X Yosemite, since it's not been mentioned much yet.Sleep behavior of Mac OS X Yosemite

    Looks like a lot of the time it sleeps about 1.25 times the time that you request and sometimes sleeps between 1 and 1.25 times the time you request. It almost never (~twice out of 1000 samples) sleeps significantly more than 1.25 times the time you request.

    Also (not shown explicitly) the 1.25 relationship seems to hold pretty well until you get below about 0.2 ms, after which it starts get a little fuzzy. Additionally, the actual time seems to settle to about 5 ms longer than you request after the amount of time requested gets above 20 ms.

    Again, it appears to be a completely different implementation of sleep() in OS X than in Windows or whichever Linux kernal Wilbert was using.

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

    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.

    Update: Like mentioned in the docs cited below, it's common to do the sleep in a loop that will make sure to go back to sleep if it wakes you up early.

    I should also mention that if you are running Ubuntu you can try out a pseudo real-time kernel (with the RT_PREEMPT patch set) by installing the rt kernel package (at least in Ubuntu 10.04 LTS).

    EDIT: Correction non-realtime Linux kernels have minimum sleep interval much closer to 1ms then 10ms but it varies in a non-deterministic manner.

    0 讨论(0)
  • 2020-11-22 11:30
    def start(self):
        sec_arg = 10.0
        cptr = 0
        time_start = time.time()
        time_init = time.time()
        while True:
            cptr += 1
            time_start = time.time()
            time.sleep(((time_init + (sec_arg * cptr)) - time_start ))
    
            # AND YOUR CODE .......
            t00 = threading.Thread(name='thread_request', target=self.send_request, args=([]))
            t00.start()
    

    Do not use a variable to pass the argument of sleep (), you must insert the calculation directly into sleep ()


    And the return of my terminal

    1 ───── 17:20:16.891 ───────────────────

    2 ───── 17:20:18.891 ───────────────────

    3 ───── 17:20:20.891 ───────────────────

    4 ───── 17:20:22.891 ───────────────────

    5 ───── 17:20:24.891 ───────────────────

    ....

    689 ─── 17:43:12.891 ────────────────────

    690 ─── 17:43:14.890 ────────────────────

    691 ─── 17:43:16.891 ────────────────────

    692 ─── 17:43:18.890 ────────────────────

    693 ─── 17:43:20.891 ────────────────────

    ...

    727 ─── 17:44:28.891 ────────────────────

    728 ─── 17:44:30.891 ────────────────────

    729 ─── 17:44:32.891 ────────────────────

    730 ─── 17:44:34.890 ────────────────────

    731 ─── 17:44:36.891 ────────────────────

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