Find out time it took for a python script to complete execution

James picture James · Jul 22, 2011 · Viewed 196.7k times · Source

I have the following code in a python script:

def fun(): 
  #Code here

fun()

I want to execute this script and also find out how much time it took to execute in minutes. How do I find out how much time it took for this script to execute ? An example would be really appreciated.

Answer

Petar Ivanov picture Petar Ivanov · Jul 22, 2011
from datetime import datetime
startTime = datetime.now()

#do something

#Python 2: 
print datetime.now() - startTime 

#Python 3: 
print(datetime.now() - startTime)