Liron Lavi
Liron Lavi

Reputation: 431

Timer in Python(simultany)

I have been looking for a timer to my project, but I only found timers that stop my code.

Does Python got an option to run a timer in a way that doesn't stop my other code?

Upvotes: 0

Views: 778

Answers (1)

Asking Questions
Asking Questions

Reputation: 667

very easy but not so sophisticated way is to write:

import time
start_time = time.time()

at the beginning of your code and at the end

print "run time (s):", time.time() - start_time

Note that in this solution the output time may be influenced by other processes running simultaniously on your computer. This is why this method is probably not the best.

A better solution would be "timeit":

https://docs.python.org/2/library/timeit.html

which can be used in very convenient fashion in ipython:

https://ipython.org/ipython-doc/dev/interactive/magics.html

Last solution I know of, is to use a profiler (very nice in spyder for example):

http://sjara.github.io/spyder-profiler/

A profiler will tell you exactly how many times a function is used during the process and whats the total time spend in a function.

Upvotes: 3

Related Questions