How to measure elapsed time in Python?
What I want is to start counting time somewhere in my code and then get the passed time, to measure the time it took to execute few function. I think I'm using the timeit module wrong, but the docs are just confusing for me.
import timeit start = timeit.timeit() print("hello") end = timeit.timeit() print(end - start)
If you just want to measure the elapsed wall-clock time between two points, you could use
import time start = time.time() print("hello") end = time.time() print(end - start)
This gives the execution time in seconds.
Another option since 3.3 might be to use
process_time, depending on your requirements. Before 3.3 it was recommended to use
time.clock (thanks Amber). However, it is currently deprecated:
On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name.
On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function
QueryPerformanceCounter(). The resolution is typically better than one microsecond.
Deprecated since version 3.3: The behaviour of this function depends on the platform: use
process_time()instead, depending on your requirements, to have a well defined behaviour.
Read more... Read less...
timeit.default_timer instead of
timeit.timeit. The former provides the best clock available on your platform and version of Python automatically:
from timeit import default_timer as timer start = timer() # ... end = timer() print(end - start) # Time in seconds, e.g. 5.38091952400282
timeit.default_timer is assigned to time.time() or time.clock() depending on OS. On Python 3.3+ default_timer is time.perf_counter() on all platforms. See Python - time.clock() vs. time.time() - accuracy?
Python 3 only:
Since time.clock() is deprecated as of Python 3.3, you will want to use
time.perf_counter() for system-wide timing, or
time.process_time() for process-wide timing, just the way you used to use
import time t = time.process_time() #do some stuff elapsed_time = time.process_time() - t
The new function
process_time will not include time elapsed during sleep.
Given a function you'd like to time,
def foo(): # print "hello" return "hello"
the easiest way to use
timeit is to call it from the command line:
% python -mtimeit -s'import test' 'test.foo()' 1000000 loops, best of 3: 0.254 usec per loop
Do not try to use
time.clock (naively) to compare the speed of functions. They can give misleading results.
PS. Do not put print statements in a function you wish to time; otherwise the time measured will depend on the speed of the terminal.
It's fun to do this with a context-manager that automatically remembers the start time upon entry to a
with block, then freezes the end time on block exit. With a little trickery, you can even get a running elapsed-time tally inside the block from the same context-manager function.
The core library doesn't have this (but probably ought to). Once in place, you can do things like:
with elapsed_timer() as elapsed: # some lengthy code print( "midpoint at %.2f seconds" % elapsed() ) # time so far # other lengthy code print( "all done at %.2f seconds" % elapsed() )
Here's contextmanager code sufficient to do the trick:
from contextlib import contextmanager from timeit import default_timer @contextmanager def elapsed_timer(): start = default_timer() elapser = lambda: default_timer() - start yield lambda: elapser() end = default_timer() elapser = lambda: end-start
And some runnable demo code:
import time with elapsed_timer() as elapsed: time.sleep(1) print(elapsed()) time.sleep(2) print(elapsed()) time.sleep(3)
Note that by design of this function, the return value of
elapsed() is frozen on block exit, and further calls return the same duration (of about 6 seconds in this toy example).
Measuring time in seconds:
from timeit import default_timer as timer from datetime import timedelta start = timer() end = timer() print(timedelta(seconds=end-start))