Notebook性能分析 - IPython

4

IPython Notebook有没有一种方法来分析其单元格的内容?

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


我最喜欢的性能分析技术是%timeit,但还有很多其他可能性,例如在这里展示的网址 - Jakob
1个回答

5

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接