Timing a single block in Jupyter with one line of code

Viewed 11005

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

2 Answers

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.

Related