[英]How do I count the change in a variable without freezing the program? [Python]
I am working on a very basic dice roll program in Python and am currently adding an ETA system (asking the program to roll the dice 1000000+ times takes a while and some may see it as a crash) and the system that I've thought up is the following: 我正在使用Python开发一个非常基本的骰子掷骰程序,目前正在添加一个ETA系统(要求该程序掷骰子1000000+次需要一些时间,有些人可能会将其视为崩溃)和我认为的系统最多是以下内容:
Since the "dice" is "rolled" by generating a random number and repeated in a for loop, if I take the variable and compare it to the variable after a second, I could do some basic maths to guess the time remaining. 由于“骰子”是通过生成随机数而“滚动”并在for循环中重复,因此,如果我获取变量并将其与变量进行比较,那么我可以做一些基本的数学运算来猜测剩余时间。
My issue is how I could wait a second between counting without freezing the program entirely (time.sleep). 我的问题是如何在计数之间等待一秒钟而不完全冻结程序(time.sleep)。
Any help would be appreciated, thank you! 任何帮助,将不胜感激,谢谢!
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")
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)
Checks the time on starting and compares a second later 检查启动时间,然后比较一秒钟
if you time difference between use something like this, 如果您在使用类似方法之间存在时间差异,
>>>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
>>>
here s
is the starting time. 这里s
的开始时间。 will get difference once process was done... it is end
流程完成后将会有所不同... end
Just to contribute. 只是为了贡献。 You can easily create a decorator and use it throughout your app. 您可以轻松创建装饰器,并在整个应用程序中使用它。 Here's an example : 这是一个例子:
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)
So each time a function, or a method, is called you can manage the time it takes to do its job. 因此,每次调用一个函数或方法时,您都可以管理完成工作所需的时间。 It's easily customizable thanks to python OO. 由于使用python OO,因此可以轻松自定义。
EDIT : I think you should refactor your code so you can evaluate time on specific logic of your program instead of the current huge function you're using. 编辑:我认为您应该重构代码,以便可以根据程序的特定逻辑而不是当前使用的庞大函数评估时间。
EDIT 2 : You could setup a CURRENT_TIME / WAIT_TIME static variable on TimeMuncher
and only log/print your time at given interval by editing __init__()
and __call__()
. 编辑2:您可以在TimeMuncher
上设置CURRENT_TIME / WAIT_TIME静态变量,并仅通过编辑__init__()
和__call__()
以给定的间隔记录/打印时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.