Measure (max) memory usage with IPython—like timeit but memit

狂风中的少年 提交于 2019-11-28 21:04:41

问题


I have a simple task: in addition to measuring the time it takes to execute a chunk of code in Python, I need to measure the amount of memory a given chunk of code needs.

IPython has a nice utility called timeit which works like this:

In [10]: timeit 3 + 3
10000000 loops, best of 3: 24 ns per loop

What I'm looking for is something like this:

In [10]: memit 3 + 3
10000000 loops, best of 3: 303 bytes per loop

I'm aware that this probably does not come built in with IPython—but I like the timeit-memit analogy.


回答1:


In fact, it already exists, as part of the pragmatically named memory_profiler package:

In [2]: %memit np.zeros(1e7)
maximum of 3: 76.402344 MB per loop

More info at https://github.com/fabianp/memory_profiler#ipython-integration

Edit: To use this, you first need to load it as an IPython extension:

%load_ext memory_profiler

To make IPython always load the memory_profiler extension upon startup, add it to the c.InteractiveShellApp.extensions list in your profile's ipython_config.py:

$ grep -C2 c.InteractiveShellApp.extensions ~/.ipython/profile_default/ipython_config.py
 # A list of dotted module names of IPython extensions to load.
 #
 c.InteractiveShellApp.extensions = [
   'autoreload',
   'memory_profiler',


来源:https://stackoverflow.com/questions/19092812/measure-max-memory-usage-with-ipython-like-timeit-but-memit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!