首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用类似IPython的timeit但memit测量(max)内存使用率

使用类似IPython的timeit但memit测量(max)内存使用率
EN

Stack Overflow用户
提问于 2013-09-30 19:14:48
回答 1查看 10.8K关注 0票数 36

我有一个简单的任务:除了测量在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的类比。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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',
票数 50
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19092812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档