使用类似IPython的timeit测量(最大)内存使用量,但请注意 [英] Measure (max) memory usage with IPython—like timeit but memit

查看:256
本文介绍了使用类似IPython的timeit测量(最大)内存使用量,但请注意的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的任务:除了测量在Python中执行一大块代码所需的时间之外,我还需要测量给定代码块所需的内存量。

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有一个很好的实用工具,名为 timeit ,其工作方式如下:

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

我知道这可能不是用IPython内置的 - 但我喜欢 timeit - memit 类比。

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

推荐答案

事实上,它已经存在,作为实用命名的 memory_profiler 包的一部分:

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

更多 https://github.com/fabianp/memory_profiler#ipython-integration 的信息

编辑:要使用此功能,首先需要将其作为IPython扩展加载:

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

%load_ext memory_profiler

要使IPython在启动时始终加载memory_profiler扩展,请添加它到您的个人资料中的 c.InteractiveShellApp.extensions 列表 ipython_config.py

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',

这篇关于使用类似IPython的timeit测量(最大)内存使用量,但请注意的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆