Tyler Heist
Tyler Heist

Reputation: 33

Get execution time of code x amount of times

What I am trying to do is run this program, get the execution time of it, and then continue to do that 9 more times. How would I go about iterating over it to get it to print out 10 different execution times? I'm not quite sure how I need to structure the program in order to accomplish this.

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))

Upvotes: 0

Views: 281

Answers (1)

Kim Souza
Kim Souza

Reputation: 172

All you need to do is create a for loop and put your code in it.

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))

Upvotes: 1

Related Questions