gota
gota

Reputation: 2659

Profiling a Notebook - IPython

Does IPython Notebook come with a method to profile its cells contents?

If not, how can I profile a part of a cell or the entire notebook?

Upvotes: 4

Views: 2504

Answers (1)

leitungswasser
leitungswasser

Reputation: 238

IPython Notebook has the very handy %prun and %%prun commands. %prun is used to profile one line of code, and %%prun is used to profile an entire cell.

You can find the documentation here: http://ipython.readthedocs.org/en/stable/interactive/magics.html#magic-prun

This is a short usage example:

%%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)

If you execute this cell, the output is something like

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)

Upvotes: 6

Related Questions