我正在尝试计时一个while循环中的另一个while循环,记录它执行所需的总时间,并在每次循环时记录所需的时间。如果可能,我需要找到一种使用我的代码实现这一目标的方法,或者接受我还不知道的不同概念。
import random
import time
import sys
def main():
looperCPU = 500
start = time.time()
while (looperCPU != 0):
#start = time.time()
#this is the computation for 3 secs
time.sleep(3)
random_number = random.randint(0,1000)
#Send to printer for processing
#.75 secs to 4.75 secs to generate a pause before printing
secondsPause = random.uniform(.75,4.75)
#This is the printer function
printerLooper = True
while printerLooper == True :
print("Sleeping for ", secondsPause, " seconds")
print(random_number)
printerLooper = False
#
print("total time taken this loop: ", time.time() - start)
looperCPU -= 1
main()
这个循环将会打印时间,但是我非常确定它没有考虑到嵌套的while循环的睡眠时间。我该如何让Python计时两个while循环,每个循环都需要执行(在这种情况下是500次)?
secondsPause
,但你实际上从未这样做。 - Gordon Bailey