我正在研究Python中一个非常基本的骰子滚动程序,目前正在添加一个ETA系统(要求程序滚动骰子1000000次需要一段时间,有些人可能会将其视为崩溃)以及我和#39;我想到了以下几点:
自"骰子"是"滚动"通过生成一个随机数并在for循环中重复,如果我接受变量并在一秒钟后将其与变量进行比较,我可以做一些基本的数学来猜测剩余的时间。
我的问题是如何在不完全冻结程序的情况下等待一秒钟(time.sleep)。
任何帮助将不胜感激,谢谢!
CODE:
import random
finished = 0
printresults = 0
dicesides = 0
rolls = 0
amountcompleted = 0
while finished !="n":
finished = 0
printresults = 0
dicesides = 0
rolls = 0
amountcompleted = 0
rollsdone = 0
countlist = []
rolls = int(input("How many times should the dice be rolled? ")) #Variable that counts how many times the dice should be rolled
dicesides = int(input("How many sides does the dice have? ")) #Variable that contains how many sides the dice has
while printresults !="y" and printresults !="n":
printresults = input("Print dice roll results as they're created? say y/n ") #If user says y, result will be printed as results are made
if printresults !="y" and printresults !="n":
print("Answer invalid")
while amountcompleted !="y" and amountcompleted !="n":
amountcompleted = input("Print the amount of rolls completed as the dice is rolled? (Reccomended when rolling over 1M times) answer y/n ")
if amountcompleted !="y" and amountcompleted !="n":
print("Answer invalid")
for counter in range(0,dicesides): #Creates list of the right length to hold results
countlist.append(0)
for counter in range (0,rolls): #Main bit of the script that actually calculates and stores amount of dice rolls
number = random.randint(1,dicesides) #"Rolls" the dice, using the dicesides variable.
if printresults == "y":
print(number) #Prints the results as they are made if enabled
if amountcompleted == "y":
(rollsdone) = int(rollsdone + 1)
print("Completed {0} rolls".format((rollsdone)))
for counter in range (0,dicesides + 1): #For variable to store the results in a list
if number == counter:
countlist[counter-1] = countlist[counter-1] + 1 #Adds 1 to the right bit of the list
for counter in range(0,dicesides):
print("Number of {0}s: {1}".format(counter + 1,countlist[counter])) #Prints results
while finished != "y" and finished != "n":
finished = input("Reroll? Answer y/n ") #Asks the user if they want to reroll with different settings
if finished != "y" and finished != "n":
print("Input invalid")
答案 0 :(得分:0)
import time, random
def roll(n):
timeStarted = time.time()
recorded = False
for x in range(1, n + 1, 1):
random.randint(1, 6)
now = time.time()
if (int(now) - int(timeStarted) == 1) and not (recorded):
recorded = True
rollsSecond = n - x
print(rollsSecond, "rolls per second")
roll(10000000)
检查开始时间并稍后比较
答案 1 :(得分:0)
如果你在使用这样的东西之间的时间差异,
>>>import time
>>> s = time.time()
>>> for i in range(10):
... print i
...
0
1
2
3
4
5
6
7
8
9
>>>end = time.time()-s
>>> print end
15.814720153808594
>>>
此处s
是开始时间。一旦流程完成就会有所不同......它是end
答案 2 :(得分:0)
只是为了贡献。您可以轻松创建装饰器并在整个应用程序中使用它。这是一个例子:
class TimeMuncher(object):
def __init__(self, f):
self.start = None
self.stop = None
self.function = f
def __call__(self, *args):
self.start = time.time() * 1000.0
self.function(*args)
self.stop = time.time() * 1000.0
self.tell_time()
def tell_time(self):
#or log..
print("{} : {} ms".format(self.function.__name__, self.stop -
self.start))
@TimeMuncher
def aFunction(name, no):
print("my function " + name + str(no) )
aFunction("hello", 2)
因此,每次调用函数或方法时,您都可以管理完成工作所需的时间。由于python OO,它可以轻松定制。
编辑:我认为您应该重构代码,这样您就可以评估程序特定逻辑的时间,而不是当前正在使用的巨大功能。编辑2:您可以在TimeMuncher
上设置CURRENT_TIME / WAIT_TIME静态变量,并且只能通过修改__init__()
和__call__()
以给定的时间间隔记录/打印您的时间。