How can I check the execution time of code in Python?

Asked 2 years ago, Updated 2 years ago, 64 views

I want to check how long it takes for the code to run. How do you measure time?

python run-time

2022-09-22 21:30

2 Answers

You can use timeit.

def function_to_test(n):
    for i in range(n):
        pass

import timeit
start = timeit.default_timer()

# Put the code to measure here
function_to_test(300000)


stop = timeit.default_timer()
print(stop - start)


2022-09-22 21:30

You can do it like this. I don't know what's different from the top comment or what's good.

import time
startTime = time.time()

# Execution Code

endTime = time.time() - startTime
print(endTime) 


2022-09-22 21:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.