Skip to content

Commit 219e9b9

Browse files
committed
Merge pull request #104 from korzeniewskipl/cell_magics
Add %%mprun and %%memit cell magics for IPython.
2 parents 018a477 + 8ad9999 commit 219e9b9

2 files changed

Lines changed: 262 additions & 177 deletions

File tree

README.rst

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ a parameter to LogFile class of memory profiler module.
269269
=====================
270270
IPython integration
271271
=====================
272-
After installing the module, if you use IPython, you can use the `%mprun`
273-
and `%memit` magics.
272+
After installing the module, if you use IPython, you can use the `%mprun`, `%%mprun`,
273+
`%memit` and `%%memit` magics.
274274

275275
For IPython 0.11+, you can use the module directly as an extension, with
276276
``%load_ext memory_profiler``
@@ -288,22 +288,36 @@ the list)::
288288
a terminal.)
289289

290290
It then can be used directly from IPython to obtain a line-by-line
291-
report using the `%mprun` magic command. In this case, you can skip
291+
report using the `%mprun` or `%%mprun` magic command. In this case, you can skip
292292
the `@profile` decorator and instead use the `-f` parameter, like
293293
this. Note however that function my_func must be defined in a file
294294
(cannot have been defined interactively in the Python interpreter)::
295295

296-
In [1] from example import my_func
296+
In [1]: from example import my_func, my_func_2
297297

298-
In [2] %mprun -f my_func my_func()
298+
In [2]: %mprun -f my_func my_func()
299+
300+
or in cell mode::
301+
302+
In [3]: %%mprun -f my_func -f my_func_2
303+
...: my_func()
304+
...: my_func_2()
299305

300306
Another useful magic that we define is `%memit`, which is analogous to
301307
`%timeit`. It can be used as follows::
302308

303-
In [1]: import numpy as np
309+
In [1]: %memit range(10000)
310+
peak memory: 21.42 MiB, increment: 0.41 MiB
311+
312+
In [2]: %memit range(1000000)
313+
peak memory: 52.10 MiB, increment: 31.08 MiB
314+
315+
or in cell mode (with setup code)::
304316

305-
In [2]: %memit np.zeros(1e7)
306-
maximum of 3: 76.402344 MB per loop
317+
In [3]: %%memit l=range(1000000)
318+
...: len(l)
319+
...:
320+
peak memory: 52.14 MiB, increment: 0.08 MiB
307321

308322
For more details, see the docstrings of the magics.
309323

@@ -316,8 +330,7 @@ file ~/.ipython/ipy_user_conf.py to add the following lines::
316330

317331
# These two are the important ones.
318332
import memory_profiler
319-
ip.expose_magic('mprun', memory_profiler.magic_mprun)
320-
ip.expose_magic('memit', memory_profiler.magic_memit)
333+
memory_profiler.load_ipython_extension(ip)
321334

322335
============================
323336
Frequently Asked Questions
@@ -373,7 +386,7 @@ Projects using memory_profiler
373386
=========
374387
Authors
375388
=========
376-
This module was written by `Fabian Pedregosa <http://fseoane.net>`_
389+
This module was written by `Fabian Pedregosa <http://fseoane.net>`_
377390
and `Philippe Gervais <https://github.com/pgervais>`_
378391
inspired by Robert Kern's `line profiler
379392
<http://packages.python.org/line_profiler/>`_.

0 commit comments

Comments
 (0)