【问题标题】:Profiling a Notebook - IPython分析笔记本 - IPython
【发布时间】:2023-04-04 00:31:02
【问题描述】:

IPython Notebook 是否带有分析其单元格内容的方法?

如果没有,我如何分析单元格的一部分或整个笔记本?

【问题讨论】:

  • 我最喜欢的分析技术是 %timeit 但还有更多的可能性,例如here

标签: profiling ipython ipython-notebook


【解决方案1】:

IPython Notebook 有非常方便的%prun%%prun 命令。 %prun 用于分析一行代码,%%prun 用于分析整个单元格。

您可以在此处找到文档:http://ipython.readthedocs.org/en/stable/interactive/magics.html#magic-prun

这是一个简短的用法示例:

%%prun -s cumulative

import numpy as np
import scipy.linalg as la

for i in range(30):
    a = np.random.randint(100,size=(1000, 1000))
    la.inv(a)

如果你执行这个单元格,输出是这样的

1053 function calls in 4.152 seconds

Ordered by: cumulative time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.001    0.001    4.152    4.152 <string>:3(<module>)
   30    3.502    0.117    3.505    0.117 basic.py:612(inv)
   30    0.646    0.022    0.646    0.022 {method 'randint' of 'mtrand.RandomState' objects}
   30    0.000    0.000    0.002    0.000 lapack.py:382(get_lapack_funcs)

【讨论】:

    猜你喜欢
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多