Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Start and stop timer [duplicate]

Tags:

python

timer

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!

like image 386
hnvasa Avatar asked Dec 21 '14 23:12

hnvasa


People also ask

How do I start and stop a timer in Python?

Python timer functionsstart() is a function that is used to initialize a timer. To end or quit the timer, one must use a cancel() function.

How do I run a Python function every 5 minutes?

With the help of the Schedule module, we can make a python script that will be executed in every given particular time interval. with this function schedule. every(5). minutes.do(func) function will call every 5 minutes.


1 Answers

Here's an answer to the same question you're asking: https://stackoverflow.com/a/2866456/4380308

import time

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

total = t1-t0

Or you can use the 'timeit' module: https://stackoverflow.com/a/2866460/4380308

timeit.timeit('foobar()', number=1000)
like image 102
John Cipponeri Avatar answered Oct 05 '22 10:10

John Cipponeri