实际上,它已经存在,作为实用名称memory_profiler
包的一部分:
In [2]: %memit np.zeros(1e7)
maximum of 3: 76.402344 MB per loop
更多信息请参见https://github.com/pythonprofilers/memory_profiler#ipython- integration
编辑:要使用此功能,您首先需要将其作为IPython扩展加载:
%load_ext memory_profiler
为了使IPython在启动时始终加载memory_profiler扩展,请将其添加到c.InteractiveShellApp.extensions
配置文件的列表中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',