我有一个简单的任务:除了测量在Python中执行代码块所需的时间之外,我还需要测量给定代码块所需的内存量。
IPython有一个名为timeit
的很好的实用程序,它的工作原理如下:
In [10]: timeit 3 + 3
10000000 loops, best of 3: 24 ns per loop
我要找的东西是这样的:
In [10]: memit 3 + 3
10000000 loops, best of 3: 303 bytes per loop
我知道这可能不是IPython内置的--但我喜欢timeit
-memit
的类比。
发布于 2013-10-01 08:16:21
实际上,它已经存在了,作为实用命名的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扩展,请将其添加到配置文件的ipython_config.py
中的c.InteractiveShellApp.extensions
列表中
$ 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
复制相似问题