我怎样才能让time.sleep缩短

时间:2017-04-11 22:33:03

标签: python time

我的程序应该玩游戏。这是我遇到问题的加载屏幕。当我运行它时,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

BTW我只有几天才进入python,所以请保持解释简单。谢谢。

2 个答案:

答案 0 :(得分:1)

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

  

参数可以是浮点数,表示更精确的睡眠时间。实际的暂停时间可能小于请求的时间,因为任何捕获的信号将在执行该信号的捕获例程后终止sleep()。此外,由于系统中其他活动的安排,暂停时间可能比任意数量的请求时间长。

Here are some measurements of accuracy

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

答案 1 :(得分:0)

您可以通过执行以下操作来测试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秒。您的个人实施可能会有所不同。