%timeit
魔术支持在线模式和单元模式下执行。使用%%timeit
调用的单元格模式(注意:百分之二符号)可用于从测量中排除某些设置代码:
%%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code code code...
但你怎么用呢?这给出了一个错误:
>>> %%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 %)?
这并不排除基准的第一行:
>>> %%timeit
... sleep(0.1)
... sleep(0.1)
...
200 ms ± 17.6 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
答案 0 :(得分:0)
将设置放在第一行,将正文放在下一行:
>>> %%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)