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

2024-03-18

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

为此,我尝试了%%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

我一直在使用Nbextension 中的执行时间 https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tree/master/src/jupyter_contrib_nbextensions/nbextensions/execute_time已经有一段时间了。太好了。

更新2021-03

截至目前,this https://stackoverflow.com/a/66663966/213525是正确的答案。本质上,%%time and %%timeit现在两者都可以正常工作。


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

别忘了 http://ipython.org/ipython-doc/dev/interactive/tutorial.html#magic-functions细胞魔法始于%%线条魔术开始于%.

%%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 taken into consideration in the next cells:[example]

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐