我试图在一个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)?