1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Python time.sleepのsleepできるのは最小何秒?

Last updated at Posted at 2022-02-18

結論

0.001秒

いちおう、スリープ時間は最小になったが、希望通りのスリープはしてくれない

検証

0.000~0.009のスリープ時間の計測

def minimam_sleep(milliseconds):
    seconds = 0.001 * milliseconds
    start_time = time.time()
    time.sleep(seconds)
    stop_time = time.time()
    time_difference = stop_time - start_time
    return time_difference 
    
for milliseconds in range(10):
    print(round(milliseconds*0.001,3), minimam_sleep(milliseconds))
0.0 0.0
0.001 0.005999088287353516
0.002 0.016000986099243164
0.003 0.01599907875061035
0.004 0.016000986099243164
0.005 0.015250682830810547
0.006 0.01600027084350586
0.007 0.015999794006347656
0.008 0.016026735305786133
0.009 0.015388250350952148

検証2

0.000 ~ 0.009
関数の呼び出し時間を0.01と仮定

def minimam_sleep(milliseconds):
    seconds = 0.001 * milliseconds
    start_time = time.time()
    time.sleep(seconds)
    stop_time = time.time()
    time_difference = stop_time - start_time
    print(time_difference-0.01)
    
for i in range(10):
    minimam_sleep(i)
指定時間  計測値
0.0 -0.01
0.001 -1.9168853759767707e-06
0.002 0.005001058578491211
0.003 0.005999317169189453
0.004 -0.00539947509765625
0.005 0.0056257152557373045
0.006 0.005166044235229492
0.007 0.007832279205322265
0.008 0.006112565994262695
0.009 0.004662504196166992

検証3

0.00 ~ 0.09
呼び出し時間 0.01と仮定

0.0 -0.01
0.01 0.010998954772949219
0.02 0.024704208374023436
0.03 0.021732797622680662
0.04 0.04448675155639648
0.05 0.0432989501953125
0.06 0.060935964584350584
0.07 0.0719084644317627
0.08 0.0872886085510254
0.09 0.09419654846191407

考察

やっぱり0.001秒は当てにならない、ミリ秒確かに最小スリープといえるがうんちです。

1
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?