我想得到的时间花在单元格执行除了原始的输出从单元格。

为此,我尝试了%%timeit -r1 -n1,但它没有公开在cell中定义的变量。

%%time适用于只包含1条语句的cell。

In[1]: %%time
       1
CPU times: user 4 µs, sys: 0 ns, total: 4 µs
Wall time: 5.96 µs
Out[1]: 1

In[2]: %%time
       # Notice there is no out result in this case.
       x = 1
       x
CPU times: user 3 µs, sys: 0 ns, total: 3 µs
Wall time: 5.96 µs

最好的方法是什么?

更新

我已经在nbeextension中使用执行时间相当长一段时间了。这是伟大的。

更新2021 - 03

到目前为止,这是正确的答案。从本质上讲,%%time和%%timeit现在都像预期的那样工作。


当前回答

当遇到麻烦时,什么意味着什么:

时间还是??时间

详情如下:

Usage, in line mode:
  %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
or in cell mode:
  %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
  code
  code...

Time execution of a Python statement or expression using the timeit
module.  This function can be used both as a line and cell magic:

- In line mode you can time a single-line statement (though multiple
  ones can be chained with using semicolons).

- In cell mode, the statement in the first line is used as setup code
  (executed but not timed) and the body of the cell is timed.  The cell
  body has access to any variables created in the setup code.

其他回答

在ipython notebook中测量单元格执行时间的最简单方法是使用ipython-autotime包。

安装包在笔记本的开始

pip install ipython-autotime

然后通过下面运行加载扩展

%load_ext autotime

一旦加载了它,在此之后运行的任何单元格都将给出该单元格的执行时间。

不要担心,如果你想关闭它,只需卸载扩展运行下面

%unload_ext autotime

这是相当简单和容易使用它,只要你想。

如果你想了解更多,可以参考ipython-autime文档或其github源代码

你可能还想查看python的剖析魔法命令% prunit给出类似-的东西

def sum_of_lists(N):
    total = 0
    for i in range(5):
        L = [j ^ (j >> i) for j in range(N)]
        total += sum(L)
    return total

然后

%prun sum_of_lists(1000000)

将返回

14 function calls in 0.714 seconds  

Ordered by: internal time      

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    5    0.599    0.120    0.599    0.120 <ipython-input-19>:4(<listcomp>)
    5    0.064    0.013    0.064    0.013 {built-in method sum}
    1    0.036    0.036    0.699    0.699 <ipython-input-19>:1(sum_of_lists)
    1    0.014    0.014    0.714    0.714 <string>:1(<module>)
    1    0.000    0.000    0.714    0.714 {built-in method exec}

我发现它在处理大块代码时很有用。

更简单的方法是使用jupyter_contrib_nbextensions包中的ExecuteTime插件。

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
jupyter nbextension enable execute_time/ExecuteTime

当遇到麻烦时,什么意味着什么:

时间还是??时间

详情如下:

Usage, in line mode:
  %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
or in cell mode:
  %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
  code
  code...

Time execution of a Python statement or expression using the timeit
module.  This function can be used both as a line and cell magic:

- In line mode you can time a single-line statement (though multiple
  ones can be chained with using semicolons).

- In cell mode, the statement in the first line is used as setup code
  (executed but not timed) and the body of the cell is timed.  The cell
  body has access to any variables created in the setup code.
import time
start = time.time()
"the code you want to test stays here"
end = time.time()
print(end - start)