Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 215
James Avatar asked Jul 22 '11 07:07

James


People also ask

Which module can say how long your code took to run?

This module provides a simple way to time small bits of Python code. It has both a Command-Line Interface as well as a callable one. It avoids a number of common traps for measuring execution times.

What does %% time mean in Python?

%%time is a magic command. It's a part of IPython. %%time prints the wall time for the entire cell whereas %time gives you the time for first line only. Using %%time or %time prints 2 values: CPU Times.


1 Answers

from datetime import datetime startTime = datetime.now()  #do something  #Python 2:  print datetime.now() - startTime   #Python 3:  print(datetime.now() - startTime) 
like image 147
Petar Ivanov Avatar answered Oct 08 '22 12:10

Petar Ivanov