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

后端 未结 11 1785
有刺的猬
有刺的猬 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

提交回复
热议问题