Python 测量 Jupyter Notebook 代码单元的运行时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43307653/
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 22:51:01  来源:igfitidea点击:

Measure runtime of a Jupyter Notebook code cell

pythontimeipythonjupyter-notebookspyder

提问by Vim

It seems that in Spyder (IPython3 Kernel) one can easily time a code cell by running the %%timeor %%timeitcommand at the top of the code cell:

似乎在 Spyder(IPython3 内核)中,可以通过在代码单元顶部运行%%time%%timeit命令轻松地对代码单元计时:

#%%
%%time # or %%timeit which measures average runtime from multiple runs
....

#%% (the previous cell ends and the next begins)

Running the above code can get the runtime of the cell defined by the pair of #%%. This is how things work in Spyder, but doesn't quite work in the Jupyter Notebook environment.

运行上面的代码可以得到#%%. 这就是 Spyder 中的工作方式,但在 Jupyter Notebook 环境中却不太适用。

In Jupyter code cells aren't defined by #%%delimiters, rather they are created by clicking a button in the menu bar. And as far as I tried, the command %%timeand %%timeitboth raise compilation error. It seems that Jupyter can't recognise them, but it's strange because my Jupyter actually uses the same IPython kernel as Spyder does. One thing that works in Jupyter is the %timeand %timeitcommands, but they can only measure the runtime of a one-line code, i.e., must be formulated like

在 Jupyter 代码中,单元格不是由#%%分隔符定义的,而是通过单击菜单栏中的按钮创建的。据我尝试,该命令%%time%%timeit两者都会引发编译错误。似乎 Jupyter 无法识别它们,但很奇怪,因为我的 Jupyter 实际上使用了与 Spyder 相同的 IPython 内核。在 Jupyter 中工作的一件事是%timeand%timeit命令,但它们只能测量一行代码的运行时间,即必须像这样表述

%time blah blah

and it turns out I can't even measure a forloop which consists of more than one line. So this method is not desirable for me. Is there just any way to evaluate a cell runtime using the magic command %time(it)and the like in Jupyter?

结果我什至无法测量for由多条线组成的循环。所以这种方法对我来说是不可取的。有没有办法%time(it)在 Jupyter 中使用魔术命令等来评估单元格运行时?

(PS: If as in Spyder I attach a %timecommand at the top of a cell it will give Wall time: 0 nsbecause there is nothing following it in that same lineand it actually measures nothing.)

(PS:如果像在 Spyder 中一样,我%time在一个单元格的顶部附加一个命令,它会给出,Wall time: 0 ns因为在同一行中没有跟随它的任何东西,它实际上什么也没有测量。)

回答by Biggsy

It depends on how you want to use the time information...

这取决于您想如何使用时间信息...

If you simply want to know how long a cell took to execute for your own knowledge, then the ExecuteTime notebook extension (https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tree/7672d429957aaefe9f2e71b15e3b78ebb9ba96d1/src/jupyter_contrib_nbextensions/nbextensions/execute_time) is a nice solution as it provides time information for all code cells automatically, meaning reduced code maintenance as you don't have to add timing code all over the place. It also writes the last executed date stamp which is useful if you're using the notebook as a scientific log-book.

如果你只是想知道一个单元花了多长时间来执行你自己的知识,那么ExecuteTime笔记本扩展(https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tree/7672d429957aaefe9f2e71b15e3b78ebb9ba96d1/src/jupyter_contrib_nbextensions/nbextensions/execute_time)是一个不错的解决方案,因为它自动为所有代码单元提供时间信息,这意味着减少了代码维护,因为您不必到处添加计时代码。它还写入上次执行的日期戳,如果您将笔记本用作科学日志,这将非常有用。

However, if you want to use the time information programatically, you will need to add code to capture the time information into a variable. As per this answer (Get time of execution of a block of code in Python 2.7), you can use the timeit module:

但是,如果要以编程方式使用时间信息,则需要添加代码以将时间信息捕获到变量中。根据这个答案(在 Python 2.7 中获取代码块的执行时间),您可以使用 timeit 模块:

import timeit
start_time = timeit.default_timer()
# code you want to evaluate
elapsed = timeit.default_timer() - start_time

Obviously, this is not as neat as using cell magic but should get the job done.

显然,这不像使用细胞魔法那么整洁,但应该可以完成工作。

As for how / if you can achieve the latter using cell magic, I don't know.

至于如何/是否可以使用细胞魔法实现后者,我不知道。

回答by Rizwan Hamid Randhawa

Please put %%time in the very start of the cell even before any comments. This worked for me.

请将 %%time 放在单元格的最开始,甚至在任何评论之前。这对我有用。

回答by Jai Mahesh

To avoid use of %% again in each cell

避免在每个单元格中再次使用 %%

Automatic cell exexution time

细胞自动执行时间

open cmd Run command one by one

一一打开cmd运行命令

  1. pip install jupyter_contrib_nbextensions
  2. jupyter contrib nbextension install --user
  3. jupyter nbextension enable spellchecker/main
  4. jupyter nbextension enable codefolding/main
  1. pip 安装 jupyter_contrib_nbextensions
  2. jupyter contrib nbextension install --user
  3. jupyter nbextension 启用拼写检查器/主
  4. jupyter nbextension 启用代码折叠/主要