获取代码的执行时间 x 次数
Get execution time of code x amount of times
我想做的是运行这个程序,得到它的执行时间,然后再继续做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))
您需要做的就是创建一个 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))
我想做的是运行这个程序,得到它的执行时间,然后再继续做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))
您需要做的就是创建一个 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))