Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timing a single block in Jupyter with one line of code

How do you time a single block of a Jupyter notebook with a single line of. Python code?

%time and %timeit don't cut it, but there must be a way to do this!

%time
%timeit
start = time.time()

## Create a Pandas dataframe from reader
df = pd.DataFrame(reader.readrows())
end = time.time()
print('Time taken ', end-start,' seconds')

Thanks, Nic

like image 856
npross Avatar asked Jul 03 '17 16:07

npross


2 Answers

Use the cell magic %%timeit (with two %) to time a whole jupyter cell, instead of just a single line. The cell magic must come before any python code in the cell.

http://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-timeit

like image 72
Håken Lid Avatar answered Sep 29 '22 12:09

Håken Lid


In Jupyter Notebook, we have a magic function timeit to time a whole cell.

%%timeit -n 10

#your code

Where 10 is the number of execution cycles.

like image 40
Sushant Agrawal Avatar answered Sep 29 '22 12:09

Sushant Agrawal