Python: Start and Stop Timer [Duplicate]

Python: Start and Stop Timer [Duplicate]

I have a code which runs for more than 1,000 iterations. I want te implement a timer that starts before the code starts executing and stops after the execution of the code is done. By this i simply want to measure the time taken for the code to complete the iterations. How do I implement such a timer in python?

Code Structure:

Start timer

//Code

Stop Timer

Thanks in advance!

0

1 Answer

Here's an answer to the same question you're asking:

import time

t0 = time.time()
code_block
t1 = time.time()

total = t1-t0

Or you can use the 'timeit' module:

timeit.timeit('foobar()', number=1000)
1
Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.