Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

IPython integration

Irixo edited this pageDec 26, 2020 ·1 revision

After installing the module, if you use IPython, you can use the%mprun,%%mprun,%memit and%%memit magics.

For IPython 0.11+, you can use the module directly as an extension, with%load_ext memory_profiler

To activate it whenever you start IPython, edit the configuration file for yourIPython profile, ~/.ipython/profile_default/ipython_config.py, to register theextension like this (If you already have other extensions, just add this one tothe list):

c.InteractiveShellApp.extensions = [    'memory_profiler',]

(If the config file doesn't already exist, runipython profile create ina terminal.)

It then can be used directly from IPython to obtain a line-by-linereport using the%mprun or%%mprun magic command. In this case, you can skipthe@profile decorator and instead use the-f parameter, likethis. Note however that function my_func must be defined in a file(cannot have been defined interactively in the Python interpreter):

In [1]: from example import my_func, my_func_2In [2]: %mprun -f my_func my_func()

or in cell mode:

In [3]: %%mprun -f my_func -f my_func_2   ...: my_func()   ...: my_func_2()

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

In [1]: %memit range(10000)peak memory: 21.42 MiB, increment: 0.41 MiBIn [2]: %memit range(1000000)peak memory: 52.10 MiB, increment: 31.08 MiB

or in cell mode (with setup code):

In [3]: %%memit l=range(1000000)   ...: len(l)   ...:peak memory: 52.14 MiB, increment: 0.08 MiB

For more details, see the docstrings of the magics.

For IPython 0.10, you can install it by editing the IPython configurationfile ~/.ipython/ipy_user_conf.py to add the following lines:

# These two lines are standard and probably already there.import IPython.ipapiip = IPython.ipapi.get()# These two are the important ones.import memory_profilermemory_profiler.load_ipython_extension(ip)

Clone this wiki locally


[8]ページ先頭

©2009-2026 Movatter.jp