How long does my Python application take to run? How long does my Python application take to run? python python

How long does my Python application take to run?


The simplest way to do it is to put:

import timestart_time = time.time()

at the start and

print "My program took", time.time() - start_time, "to run"

at the end.


To get the best result on different platforms:

from timeit import default_timer as timer# START MY TIMERstart = timer()code for applicationmore codemore codeetc# STOP MY TIMERelapsed_time = timer() - start # in seconds

timer() is a time.perf_counter() in Python 3.3 and time.clock()/time.time() in older versions on Windows/other platforms correspondingly.


If you're running Mac OS X or Linux, just use the time utility:

$ time python script.pyreal    0m0.043suser    0m0.027ssys     0m0.013s

If not, use the time module:

import timestart_time = time.time()# ...print time.time() - start_time, 's'