分析笔记本 - IPython

got*_*ota 4 profiling ipython jupyter-notebook

IPython Notebook 是否提供了分析其单元格内容的方法?

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

小智 6

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)
Run Code Online (Sandbox Code Playgroud)

如果执行此单元格,输出类似于

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)
Run Code Online (Sandbox Code Playgroud)