Is there a way to store the execution time of a cell in a variable?

1k views Asked by At

Although ipython-autotime package can display the running time of every cell, using the following code

!pip install ipython-autotime %load_ext autotime

I couldn't find a way to store it in a variable.

I am trying to find a way to store the running time of all cells to variables, and finally export the values for tabulation.

1

There are 1 answers

0
aman-aman On

You can use the time module to do the same task but in a different fashion.

import time

#at the start of cell, define <tic> variable
tic = time.time()

"""Write your python logic here"""

print("hello world")

"""Your logic ends here"""

#at the end of cell, define <toc> variable
toc = time.time()

#store execution time in variable. Note the unit of the result is seconds
time_taken = toc - tic