在 ipython notebook 中测量单元格执行时间的简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32565829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:49:10  来源:igfitidea点击:

Simple way to measure cell execution time in ipython notebook

pythonipythonipython-notebookjupyter

提问by colinfang

I would like to get the time spent on the cell execution in addition to the original output from cell.

除了单元格的原始输出之外,我还想获得单元格执行所花费的时间。

To this end, I tried %%timeit -r1 -n1but it doesn't expose the variable defined within cell.

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

%%timeworks for cell which only contains 1 statement.

%%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

What's the best way to do it?

最好的方法是什么?

Update

更新

I have been using Execute Time in Nbextensionfor quite some time now. It is great.

在 Nbextension 中使用Execute Time已经有一段时间了。太好了。

采纳答案by Philipp Schwarz

Use cell magic and this project on github by Phillip Cloud:

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

Load it by putting this at the top of your notebook or put it in your config file if you always want to load it by default:

通过将它放在笔记本顶部来加载它,或者如果您总是希望默认加载它,则将它放在您的配置文件中:

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

If loaded, every output of subsequent cell execution will include the time in min and sec it took to execute it.

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

回答by ryanmc

%timeand %timeitnow come part of ipython's built-in magic commands

%time%timeit现在来IPython中的一部分内置的魔法命令

回答by Salvador Dali

The only way I found to overcome this problem is by executing the last statement with print.

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

Do not forget thatcell magic starts with %%and line magic starts with %.

不要忘记单元魔术以 开头,%%线魔术以 开头%

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

Notice that any changes performed inside the cell are not taken into consideration in the next cells, something that is counter intuitive when there is a pipeline: an example

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

回答by eafit

This is not exactly beautiful but without extra software

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

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))

Then you can run it like:

然后你可以像这样运行它:

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

% 49999995000000
% runtime: 0:00:00.338492

回答by blehman

Sometimes the formatting is different in a cell when using print(res), but jupyter/ipython comes with a display. See an example of the formatting difference using pandas below.

有时使用 时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)

The displaystatement can preserve the formatting. screenshot

display语句可以保留格式。 screenshot

回答by Harry_pb

I simply added %%timeat the beginning of the cell and got the time. You may use the same on Jupyter Spark cluster/ Virtual environment using the same. Just add %%timeat the top of the cell and you will get the output. On spark cluster using Jupyter, I added to the top of the cell and I got output like below:-

我只是%%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

回答by vForce

An easier way is to use ExecuteTime plugin in jupyter_contrib_nbextensions package.

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

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

回答by markroxor

you may also want to look in to python's profiling magic command %prunwhich gives something like -

您可能还想查看 python 的分析魔术命令%prun,它给出了类似的东西 -

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

then

然后

%prun sum_of_lists(1000000)

will return

将返回

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}

I find it useful when working with large chunks of code.

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

回答by Mostafa Gazar

You can use timeitmagic function for that.

您可以timeit为此使用魔术功能。

%timeit CODE_LINE

Or on the cell

或者在细胞上

%%timeit 

SOME_CELL_CODE

Check more IPython magic functions at https://nbviewer.jupyter.org/github/ipython/ipython/blob/1.x/examples/notebooks/Cell%20Magics.ipynb

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

回答by mina

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