简体   繁体   English

我怎样才能让 time.sleep 更短

[英]How Can I Make time.sleep Shorter

My programs supposed to play a game.我的程序应该玩游戏。 It's the loading screen I'm having problems with.这是我遇到问题的加载屏幕。 When I run it, time.sleep acts as if it's sleeping 0.1 seconds, instead of the much smaller numbers I input into it.当我运行它时, time.sleep就好像它在休眠0.1秒,而不是我输入的小得多的数字。 Why is this?为什么是这样? Is there a way I can make the delays shorter?有什么办法可以缩短延迟吗? Here's the code:这是代码:

import os
import random
import time
import sys

def mywrite(line2,line1,t,s,x='small'):
    if x=='small':
        x=0.0000000000000000000000000000000000000000000000000000000000000000000000001
    else:
        x=random.random()*t+s
    word=''
    for c in line1:
        if line1.index(c)<len(line1)-1:
            print(line2)
            word=word+c
            print(word)
            time.sleep(x)
            os.system('cls')
        else:
            print(line2)
            word=word+c
            print(word,' \n')
            time.sleep(x)
mywrite('__________________________________________________________\n',' %33s'%'Scrambled',0.005,0.1,'random')
print('    Press "a" to play %30s'%'Press "s" to exit')
print('__________________________________________________________')
start=input()
if start=='a':
    permission=1
if start=='s':
    permission=0
if permission==0:
    sys.exit()
if permission==1:
    print("Choose Difficulty")
    print('Easy        -Press a')
    print('Hard        -Press b')
    print('Insane      -Press c')
    diff=input()
y=0
while permission==1:
    os.system('cls')
    mywrite('''





  _                       _   _                 
 (_)   ___     __ _    __| | | |  _ __     __ _ 
 | |  / _ \\   / _` |  / _` | | | | '_ \\   / _` |
 | | | (_) | | (_| | | (_| | | | | | | | | (_| |
 |_|  \\___/   \\__, |  \\__,_| |_| |_| |_|  \\__,_|
              |___/                                                       
    ''', 0.005, 0.001)
    time.sleep(2)
    os.system('cls')
    mywrite('''
  _                           _                 
 (_)           __ _          | |           __ _ 
 | |          / _` |         | |          / _` |
 | |         | (_| |         | |         | (_| |
 |_|          \__, |         |_|          \__,_|
              |___/                             
                          _                 
       ___             __| |      _ __  
      / _ \           / _` |     | '_ \ 
     | (_) |         | (_| |     | | | |
      \___/           \__,_|     |_| |_|

    ''', 0.005, 0.001)
    time.sleep(2)
    os.system('cls')
    mywrite('''
  _                           _                 
 | |           __ _          (_)           __ _ 
 | |          / _` |         | |          / _` |
 | |         | (_| |         | |         | (_| |
 |_|          \__,_|         |_|          \__, |
                                          |___/ 
                          _                 
       ___             __| |      _ __  
      / _ \           / _` |     | '_ \ 
     | (_) |         | (_| |     | | | |
      \___/           \__,_|     |_| |_|

    ''', 0.005, 0.001)
    time.sleep(2)
    os.system('cls')
    mywrite('''





  _                       _   _                 
 | |   ___     __ _    __| | (_)  _ __     __ _ 
 | |  / _ \   / _` |  / _` | | | | '_ \   / _` |
 | | | (_) | | (_| | | (_| | | | | | | | | (_| |
 |_|  \___/   \__,_|  \__,_| |_| |_| |_|  \__, |
                                          |___/ 

    ''', 0.005, 0.001)
    time.sleep(4)
    os.system('cls') 
    if y==2:
        break

If that's too long, here's the part that contains the problem:如果太长,这里是包含问题的部分:

import os
import random
import time
import sys

def mywrite(line2,line1,t,s,x='small'):
    if x=='small':
        x=0.0000000000000000000000000000000000000000000000000000000000000000000000001
    else:
        x=random.random()*t+s
    word=''
    for c in line1:
        if line1.index(c)<len(line1)-1:
            print(line2)
            word=word+c
            print(word)
            time.sleep(x)
            os.system('cls')
        else:
            print(line2)
            word=word+c
            print(word,' \n')
            time.sleep(x)
while permission==1:
    os.system('cls')
    mywrite('''





  _                       _   _                 
 (_)   ___     __ _    __| | | |  _ __     __ _ 
 | |  / _ \\   / _` |  / _` | | | | '_ \\   / _` |
 | | | (_) | | (_| | | (_| | | | | | | | | (_| |
 |_|  \\___/   \\__, |  \\__,_| |_| |_| |_|  \\__,_|
              |___/                                                       
    ''', 0.005, 0.001)
    time.sleep(2)
    os.system('cls')
    mywrite('''
  _                           _                 
 (_)           __ _          | |           __ _ 
 | |          / _` |         | |          / _` |
 | |         | (_| |         | |         | (_| |
 |_|          \__, |         |_|          \__,_|
              |___/                             
                          _                 
       ___             __| |      _ __  
      / _ \           / _` |     | '_ \ 
     | (_) |         | (_| |     | | | |
      \___/           \__,_|     |_| |_|

    ''', 0.005, 0.001)
    time.sleep(2)
    os.system('cls')
    mywrite('''
  _                           _                 
 | |           __ _          (_)           __ _ 
 | |          / _` |         | |          / _` |
 | |         | (_| |         | |         | (_| |
 |_|          \__,_|         |_|          \__, |
                                          |___/ 
                          _                 
       ___             __| |      _ __  
      / _ \           / _` |     | '_ \ 
     | (_) |         | (_| |     | | | |
      \___/           \__,_|     |_| |_|

    ''', 0.005, 0.001)
    time.sleep(2)
    os.system('cls')
    mywrite('''





  _                       _   _                 
 | |   ___     __ _    __| | (_)  _ __     __ _ 
 | |  / _ \   / _` |  / _` | | | | '_ \   / _` |
 | | | (_) | | (_| | | (_| | | | | | | | | (_| |
 |_|  \___/   \__,_|  \__,_| |_| |_| |_|  \__, |
                                          |___/ 

    ''', 0.005, 0.001)
    time.sleep(4)
    os.system('cls') 
    if y==2:
        break

BTW I'm only a few days into python, so please keep the explanations simple.顺便说一句,我对 python 的使用只有几天,所以请保持简单的解释。 Thank you.谢谢你。

Actually, time.sleep tries to achieve the desired sleep time but does not guarantee it. 实际上, time.sleep尝试达到所需的睡眠时间,但并不能保证达到目标。 From the documentation : 文档中

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. 实际的暂停时间可能少于请求的暂停时间,因为任何捕获到的信号都会在执行该信号的捕获例程后终止sleep()。 Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system. 而且,由于系统中其他活动的调度,暂停时间可能比请求的时间长任意数量。

Here are some measurements of accuracy . 这是一些准确性的度量

There's no cross-platform way to achieve sub-millisecond timing. 没有跨平台的方法可以实现亚毫秒级的计时。 Eg on windows you cannot get better than 1ms. 例如,在Windows上,您无法获得优于1ms的效果。 See https://mail.python.org/pipermail/python-win32/2006-August/004906.html . 参见https://mail.python.org/pipermail/python-win32/2006-August/004906.html On Linux, there may be ways using nanosleep(). 在Linux上,可能存在使用nanosleep()的方法。 See also Python: high precision time.sleep 另请参见Python:高精度time.sleep

You can test minimum resolution of time.sleep by doing something like: 您可以通过以下方法测试time.sleep的最低分辨率:

from datetime import datetime
import time

def test_time(delay):
    start = datetime.now()
    time.sleep(delay)
    finish = datetime.now()
    diff = finish - start
    return diff.total_seconds()

Define that in your REPL (IDLE or terminal or whatever you use) and test it out. 在REPL(空闲或终端或您使用的任何设备)中进行定义,然后进行测试。 On my system, using Python 3.6.1, I get: 在我的系统上,使用Python 3.6.1,我得到:

>>> test_time(1)
1.014032
>>> test_time(1)
1.014032
>>> test_time(0.1)
0.109204
>>> test_time(0.0001)
0.015601
>>> test_time(0.0002)
0.015601
>>> test_time(0.000000000001)
0.0156
>>> test_time(0.000000000001)
0.0156
>>> test_time(0.0000000000000000000000001)
0.015601
>>>

So the minimum resolution of time.sleep is somewhere around 0.0156 seconds on my machine. 所以,最小分辨率time.sleep是某处大约0.0156秒我的机器上。 Your individual implementation may vary. 您的具体实现可能有所不同。

i found that if you use:我发现如果你使用:

timer = time.time()
    while timer + wait > time.time(): #wait is your sleep time
      pass

the result is better then when you use:结果比使用时更好:

time.sleep(wait)

but i don't know why但我不知道为什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM