【问题标题】:How Can I Make time.sleep Shorter我怎样才能让 time.sleep 更短
【发布时间】:2017-09-07 12:15:26
【问题描述】:

我的程序应该玩游戏。这是我遇到问题的加载屏幕。当我运行它时,time.sleep 表现得好像它正在休眠 0.1 秒,而不是我输入的小得多的数字。为什么是这样?有什么办法可以缩短延误时间吗?代码如下:

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

如果太长,这里是包含问题的部分:

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

顺便说一句,我才刚接触 python 几天,所以请保持解释简单。谢谢。

【问题讨论】:

  • 你应该避免写 0.00000000000000000000000000000000000000000000000000000000000000000000000001 并了解E-notation
  • 我知道。我只是对那个 0 感到沮丧并发送了垃圾邮件。XD
  • 离题,但为什么第一个“加载”艺术退格被转义,而其余的却没有?如果您不需要转义,请不要放置转义。如果这样做,请将它们放在任何地方(或者最好使用原始字符串表示法r"something\with\backslashes"
  • 当我在没有它们的情况下运行第一个时,它没有正确显示。我把它们放进去后,它们都工作得很好。我的老师提到了一些关于它的事情,因为退格键有时会导致问题。

标签: python time


【解决方案1】:

您可以通过以下方式测试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()

在您的 REPL(IDLE 或终端或您使用的任何东西)中定义它并测试它。在我的系统上,使用 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
>>>

所以time.sleep 的最小分辨率在我的机器上大约是 0.0156 秒。您的个人实施可能会有所不同。

【讨论】:

  • 谢谢。但是您知道 time.sleep 的替代方法吗?最小分辨率约为 0.0001 左右的?
  • 您需要这么小的分辨率这一事实表明您不应该为此使用 Python。为什么你需要一次睡十分之一毫秒?
  • 另外:您使用的是什么操作系统?您更可能受到架构的限制,而不是您选择的编程语言!见this similar question
  • e. G。您可以在 100000 左右的范围内进行 for 循环,但这似乎不是一个好的解决方案,因为毕竟您只会在您的机器上获得所需的行为,而在另一台机器上会发生灾难
  • 我正在使用 Windows。这是一个 10 年级的技术项目,所以我希望我的老师不要厌倦观看冗长的加载屏幕。是的,我向她寻求帮助,但她说她不知道:/
【解决方案2】:

实际上,time.sleep 试图达到所需的睡眠时间,但不能保证。来自documentation

参数可以是一个浮点数,表示更精确的睡眠时间。实际的挂起时间可能少于请求的时间,因为任何捕获的信号都会在执行该信号的捕获例程后终止 sleep()。此外,由于系统中其他活动的调度,暂停时间可能比请求的时间长。

Here are some measurements of accuracy.

没有实现亚毫秒级计时的跨平台方法。例如。在 Windows 上,你不能超过 1 毫秒。见https://mail.python.org/pipermail/python-win32/2006-August/004906.html。在 Linux 上,可能有使用 nanosleep() 的方法。另见Python: high precision time.sleep

【讨论】:

    【解决方案3】:

    我发现如果你使用:

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

    结果比你使用时更好:

    time.sleep(wait)
    

    但我不知道为什么

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2011-06-14
    • 2012-08-02
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多