我怎样才能让 time.sleep 更短 [英] How Can I Make time.sleep Shorter

查看:100
本文介绍了我怎样才能让 time.sleep 更短的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

导入操作系统随机导入导入时间导入系统def mywrite(line2,line1,t,s,x='small'):如果 x=='小':x=0.00000000000000000000000000000000000000000000000000000000000000000000001别的:x=random.random()*t+s字=''对于第 1 行中的 c:如果 line1.index(c)<len(line1)-1:打印(第2行)字=字+c打印(字)时间.sleep(x)os.system('cls')别的:打印(第2行)字=字+c打印(字,'\n')时间.sleep(x)mywrite('__________________________________________________________\n',' %33s'%'Scrambled',0.005,0.1,'random')print('按a"播放%30s'%'按s"退出')打印('__________________________________________________________')开始=输入()如果开始=='a':权限=1如果开始==':权限=0如果权限==0:系统退出()如果权限==1:print("选择难度")打印('简单-按一个')打印('硬压 b')print('Insane -Press c')差异=输入()y=0而权限==1:os.system('cls')我的写('''_ _ _(_) ___ __ _ __||||_ __ __ _||/_ \\/_` |/_` ||||'_ \\/_` ||||(_) ||(_| | | (_| | | | | | | | (_| ||_|\\___/\\__, |\\__,_||_||_||_|\\__,_||___/''', 0.005, 0.001)时间.sleep(2)os.system('cls')我的写('''_ _(_) _ _ ||_ _||/_` |||/_` ||||(_| | | | | (_| ||_|\__, ||_|\__,_||___/____ __||_ __/_ \/_` ||'_ \|(_) ||(_| | | | | |\___/\__,_||_||_|''', 0.005, 0.001)时间.sleep(2)os.system('cls')我的写('''_ _||__ _ (_) __ _||/_` |||/_` ||||(_| | | | | (_| ||_|\__,_||_|\__, ||___/____ __||_ __/_ \/_` ||'_ \|(_) ||(_| | | | | |\___/\__,_||_||_|''', 0.005, 0.001)时间.sleep(2)os.system('cls')我的写('''_ _ _||___ __ _ __||(_) _ __ __ _||/_ \/_` |/_` ||||'_ \/_` ||||(_) ||(_| | | (_| | | | | | | | (_| ||_|\___/\__,_|\__,_||_||_||_|\__, ||___/''', 0.005, 0.001)时间.sleep(4)os.system('cls')如果y==2:休息

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

导入操作系统随机导入导入时间导入系统def mywrite(line2,line1,t,s,x='small'):如果 x=='小':x=0.00000000000000000000000000000000000000000000000000000000000000000000001别的:x=random.random()*t+s字=''对于第 1 行中的 c:如果 line1.index(c)<len(line1)-1:打印(第2行)字=字+c打印(字)时间.sleep(x)os.system('cls')别的:打印(第2行)字=字+c打印(字,'\n')时间.sleep(x)而权限==1:os.system('cls')我的写('''_ _ _(_) ___ __ _ __||||_ __ __ _||/_ \\/_` |/_` ||||'_ \\/_` ||||(_) ||(_| | | (_| | | | | | | | (_| ||_|\\___/\\__, |\\__,_||_||_||_|\\__,_||___/''', 0.005, 0.001)时间.sleep(2)os.system('cls')我的写('''_ _(_) _ _ ||_ _||/_` |||/_` ||||(_| | | | | (_| ||_|\__, ||_|\__,_||___/____ __||_ __/_ \/_` ||'_ \|(_) ||(_| | | | | |\___/\__,_||_||_|''', 0.005, 0.001)时间.sleep(2)os.system('cls')我的写('''_ _||__ _ (_) __ _||/_` |||/_` ||||(_| | | | | (_| ||_|\__,_||_|\__, ||___/____ __||_ __/_ \/_` ||'_ \|(_) ||(_| | | | | |\___/\__,_||_||_|''', 0.005, 0.001)时间.sleep(2)os.system('cls')我的写('''_ _ _||___ __ _ __||(_) _ __ __ _||/_ \/_` |/_` ||||'_ \/_` ||||(_) ||(_| | | (_| | | | | | | | (_| ||_|\___/\__,_|\__,_||_||_||_|\__, ||___/''', 0.005, 0.001)时间.sleep(4)os.system('cls')如果y==2:休息

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

解决方案

您可以通过执行以下操作来测试 time.sleep 的最小分辨率:

from datetime import datetime导入时间def test_time(延迟):开始 = datetime.now()时间.睡眠(延迟)完成 = datetime.now()差异 = 完成 - 开始返回 diff.total_seconds()

在你的 REPL(空闲或终端或你使用的任何东西)中定义它并测试它.在我的系统上,使用 Python 3.6.1,我得到:

<预><代码>>>>测试时间(1)1.014032>>>测试时间(1)1.014032>>>测试时间(0.1)0.109204>>>测试时间(0.0001)0.015601>>>测试时间(0.0002)0.015601>>>测试时间(0.0000000000001)0.0156>>>测试时间(0.0000000000001)0.0156>>>测试时间(0.0000000000000000000000001)0.015601>>>

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

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. 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. Thank you.

解决方案

You can test minimum resolution of time.sleep by doing something like:

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. On my system, using Python 3.6.1, I get:

>>> 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. Your individual implementation may vary.

这篇关于我怎样才能让 time.sleep 更短的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆