我想做的是运行该程序,获取其执行时间,然后继续执行9次以上。我将如何对其进行迭代以使其打印出10个不同的执行时间?我不太确定要如何完成程序结构。
import time
start_time = time.time()
def fibonacci():
previous_num, result = 0, 1
user = 1000
iteration = 10
while len(str(result)) < user:
previous_num, result = result, previous_num + result
while iteration != 0:
iteration -= 1
end = time.time()
print(start_time - end)
return result
print(fibonacci())
print("--- %s seconds ---" % (time.time() - start_time))
答案 0 :(得分:1)
您需要做的就是创建一个for循环并将您的代码放入其中。
import time
def fibonacci(start_time):
previous_num, result = 0, 1
user = 1000
iteration = 10
while len(str(result)) < user:
previous_num, result = result, previous_num + result
while iteration != 0:
iteration -= 1
end = time.time()
print(start_time - end)
return result
for i in range(0, 10):
start_time = time.time()
print(fibonacci(start_time))
print("--- %s seconds ---" % (time.time() - start_time))