在ipython笔记本中测量单元执行时间的简单方法

问题描述 投票:116回答:9

除了来自单元格的原始输出之外,我还想花费在单元格执行上花费的时间。

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

%%time适用于仅包含1个语句的单元格。

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

最好的方法是什么?

Update

我一直在使用Execute Time in Nbextension很长一段时间了。太棒了。

python ipython ipython-notebook jupyter
9个回答
29
投票

在Phillip Cloud的github上使用cell magic和这个项目:

如果您希望在默认情况下加载它,请将其放在笔记本顶部或将其放入配置文件中来加载它:

%install_ext https://raw.github.com/cpcloud/ipython-autotime/master/autotime.py
%load_ext autotime

如果加载,后续单元格执行的每个输出将包括执行它所花费的时间(分钟和秒)。


1
投票

你可以使用timeit魔术功能。

%timeit CODE_LINE

或者在细胞上

%%timeit SOME_CELL_CODE

https://nbviewer.jupyter.org/github/ipython/ipython/blob/1.x/examples/notebooks/Cell%20Magics.ipynb上查看更多IPython魔术函数


0
投票

遇到麻烦时意味着什么:

?%timeit??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.

342
投票

我发现克服这个问题的唯一方法是使用print执行最后一个语句。

Do not forget that细胞魔法开始于%%,线魔法开始于%

%%time
clf = tree.DecisionTreeRegressor().fit(X_train, y_train)
res = clf.predict(X_test)
print(res)

请注意,在单元格内执行的任何更改都不会在下一个单元格中被考虑,这在存在管道时是反直觉的:an example


48
投票

%time%timeit现在成为ipython的内置magic commands的一部分


19
投票

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

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

11
投票

我只是在单元格的开头添加了%%time并得到了时间。您可以在Jupyter Spark集群/虚拟环境中使用相同的内容。只需在单元格顶部添加%%time即可获得输出。在使用Jupyter的spark集群上,我添加到单元格的顶部,我得到如下输出: -

[1]  %%time
     import pandas as pd
     from pyspark.ml import Pipeline
     from pyspark.ml.classification import LogisticRegression
     import numpy as np
     .... code ....

Output :-

CPU times: user 59.8 s, sys: 4.97 s, total: 1min 4s
Wall time: 1min 18s

7
投票

有时使用print(res)时单元格格式不同,但jupyter / ipython附带display。使用下面的pandas查看格式差异的示例。

%%time
import pandas as pd 
from IPython.display import display

df = pd.DataFrame({"col0":{"a":0,"b":0}
              ,"col1":{"a":1,"b":1}
              ,"col2":{"a":2,"b":2}
             })

#compare the following
print(df)
display(df)

display语句可以保留格式。 screenshot


6
投票

这不是很漂亮,但没有额外的软件

class timeit():
    from datetime import datetime
    def __enter__(self):
        self.tic = self.datetime.now()
    def __exit__(self, *args, **kwargs):
        print('runtime: {}'.format(self.datetime.now() - self.tic))

然后你可以运行它:

with timeit():
    # your code, e.g., 
    print(sum(range(int(1e7))))

% 49999995000000
% runtime: 0:00:00.338492

2
投票
import time
start = time.time()
"the code you want to test stays here"
end = time.time()
print(end - start)

1
投票

你可能还想查看python的profiling magic命令%prunwhich给出类似的东西 -

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}

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

© www.soinside.com 2019 - 2024. All rights reserved.