在 ipython notebook 中测量单元格执行时间的简单方法 [英] Simple way to measure cell execution time in ipython notebook

查看:26
本文介绍了在 ipython notebook 中测量单元格执行时间的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

In[1]: %%time1CPU 时间:用户 4 µs,系统:0 ns,总计:4 µs挂壁时间:5.96 µs出[1]:1在[2]:%%时间# 注意在这种情况下没有输出结果.x = 1XCPU 时间:用户 3 µs,系统:0 ns,总计:3 µs挂壁时间:5.96 µs

最好的方法是什么?

更新

我一直在使用

%time 衡量某件事运行所需的时间.报告长期运行的操作比进行低级优化更好.

%%timeit 是一个基准测试工具,它反复运行语句以给出某些语句的平均运行时间以及标准偏差.由于语句重复执行的方式,在%%timeit单元格中创建的变量在其他单元格中不可用.

%%timeit 使用 python timeit 模块.文档说,

<块引用>

它避免了用于测量执行时间的常见陷阱的数量.另见蒂姆·彼得斯Python Cookbook 中算法"一章的介绍,由奥莱利.

希望该模块仍然相关,如它所指的参考 描述了以下问题,例如 (1) Windows 98 的解决方法,每秒仅更新 time.time() 18.2 次,以及 (2) 干扰所有将语句放在一行上,以避免增加行号计数器的字节码开销.


目前评分最高的答案,以及其他一些过时的答案——应该删除,因为它们现在高度误导确实有用的评论表明这些答案是不正确的:

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 -n1 but it doesn't expose the variable defined within cell.

%%time works for cell which only contains 1 statement.

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 Nbextension for quite some time now. It is great.

Update 2021-03

As of now, this is the correct answer. Essentially, %%time and %%timeit both now work as one would expect.

解决方案

That was only a problem in old versions.

All you need to do now is put %%time at the top of the cell.

%time measures how long it took something to run. It’s better for reporting on long-running operations than for doing low-level optimization.

%%timeit is a benchmarking tool that runs statements over and over to give the average runtime for some statements, as well as the standard deviation. Because of the way in which the statements are repeatedly executed, the variables created in %%timeit cells are not available in other cells.

%%timeit uses the python timeit module. The docs for that say,

It avoids a number of common traps for measuring execution times. See also Tim Peters’ introduction to the "Algorithms" chapter in the Python Cookbook, published by O’Reilly.

I hope that that module is still relevant, as the reference it refers to describes issues such as (1) workarounds for Windows 98 only updating time.time() 18.2 times per second, and (2) jamming all the statements onto one line to avoid the bytecode overhead of incrementing the line number counter.


The currently top-rated answer, as well as some of the other outdated ones—which should be deleted because they are now highly misleadingdo have useful comments indicating that those answers are not correct:

这篇关于在 ipython notebook 中测量单元格执行时间的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆