The %timeit
magic supports execution in line mode and cell mode. Using the cell mode, invoked with %%timeit
(note: two percent symbols), can be used to exclude some setup code from the measurement:
%%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code code code...
But how do you use it? This gives an error:
>>> %%timeit sleep(0.1); sleep(0.1)
...
UsageError: %%timeit is a cell magic, but the cell body is empty.
Did you mean the line magic %timeit (single %)?
And this doesn't exclude the first line from the benchmark:
>>> %%timeit
... sleep(0.1)
... sleep(0.1)
...
200 ms ± 17.6 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
Put the setup on the first line, and the body on the next line(s):
>>> %%timeit sleep(0.1)
... sleep(0.2)
... sleep(0.3)
...
500 ms ± 14.1 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With