作用:memory_profiler是用来分析每行代码的内存使用情况

使用方法一:

   1.在函数前添加 @profile

2.运行方式: python -m memory_profiler memory_profiler_test.py

  此方法缺点:在调试 和 实际项目运行时 要 增删 @profile 此装饰器

代码如下:

 #coding:utf8

 @profile
def test1():
c=0
for item in xrange(100000):
c+=1
print c if __name__=='__main__':
test1()

输出结果:

rgc@rgc:~/baidu_eye/carrier/test$ python -m memory_profiler memory_profiler_test.py
100000
Filename: memory_profiler_test.py Line # Mem usage Increment Line Contents
================================================
5 21.492 MiB 21.492 MiB @profile
6 def test1():
7 21.492 MiB 0.000 MiB c=0
8 21.492 MiB 0.000 MiB for item in xrange(100000):
9 21.492 MiB 0.000 MiB c+=1
10 21.492 MiB 0.000 MiB print c

名词含义为

  Mem usage: 内存占用情况

  Increment: 执行该行代码后新增的内存

使用方法二:

  1.先导入:    from memory_profiler import profile

2.函数前加装饰器:   @profile(precision=4,stream=open('memory_profiler.log','w+'))

            参数含义:precision:精确到小数点后几位

                 stream:此模块分析结果保存到 'memory_profiler.log' 日志文件。如果没有此参数,分析结果会在控制台输出

  运行方式:直接跑此脚本  python memory_profiler_test.py

  此方法优点:解决第一种方法的缺点,在 不需要 分析时,直接注释掉此行

 #coding:utf8
from memory_profiler import profile @profile(precision=4,stream=open('memory_profiler.log','w+'))
# @profile
def test1():
c=0
for item in xrange(100000):
c+=1
print c if __name__=='__main__':
test1()

使用方法三:

  脚本代码和方法二一样,但是 运行方式不同

  mprof run memory_profiler_test.py       : 分析结果会保存到一个 .dat格式文件中

  mprof plot                                              : 把结果以图片到方式显示出来(直接在本目录下运行此命令即可,程序会自动找出.dat文件) (要安装  pip install matplotlib

mprof clean                                           : 清空所有 .dat文件

memory_profiler的使用的更多相关文章

  1. psutil 是因为该包能提升 memory_profiler 的性能

    python 性能分析入门指南 一点号数据玩家昨天 限时干货下载:添加微信公众号"数据玩家「fbigdata」" 回复[7]免费获取[完整数据分析资料!(包括SPSS.SAS.SQ ...

  2. python 内存监控模块之memory_profiler

    0. memory_profiler是干嘛的 This is a python module for monitoring memory consumption of a process as wel ...

  3. Python的7种性能测试工具:timeit、profile、cProfile、line_profiler、memory_profiler、PyCharm图形化性能测试工具、objgraph

    1.timeit: >>> import timeit >>> def fun(): ): a = i * i >>> timeit.timeit ...

  4. python--cProfile,memory_profiler,psutil

    关于测试代码用了多长时间,我们之前介绍了timeit.相较于timeit,python中还有一个更加强大的模块,cProfile模块 (提到cProfile,其实还有一个profile,但profil ...

  5. python的memory_profiler模块使用

    本文主要介绍了python内存分析工具: memory_profiler,可以展示每一行代码执行所增加的内存,方便做内存调优和排除bug memory_profiler是第三方模块,需要安装才能使用 ...

  6. 使用memory_profiler异常

    在使用memory_profiler模块0.55.0版本执行命令诊断程序内存用量时,遇到下面错误: C:\Users\Chen\Desktop\python_doc\第四模块课件>python ...

  7. python性能检测工具整理

    python 运行后出现core dump产生core.**文件,可通过gdb来调试 Using GDB with a core dump having found build/python/core ...

  8. python 检查内存

    ################################# 测试函数运行内存# coding=utf-8# pip install memory_profiler# pip install p ...

  9. 转帖:Python应用性能分析指南

    原文:A guide to analyzing Python performance While it’s not always the case that every Python program ...

随机推荐

  1. excle中表引用

  2. PHP7 MongDB 安装与使用

    我们使用 pecl 命令来安装: /usr/local/php7/bin/pecl install mongodb 执行成功后,会输出以下结果: …… Build process completed ...

  3. HBase应用快速开发

    有人说过“让Hadoop开发像家庭作业一样简单”,容器技术的出现让这成为可能,可以用Docker封装HBase运行环境,通过统一的接口来运行.本文将介绍如何在十分钟内跑起你的HBase应用. 首先,我 ...

  4. Arch Linux 硬盘引导-联网安装

    Arch Linux 硬盘引导-联网安装 ============https://www.archlinux.org/https://wiki.archlinux.org/https://wiki.a ...

  5. 学习笔记CB002:词干提取、词性标注、中文切词、文档分类

    英文词干提取器,import nltk,porter = nltk.PorterStemmer(),porter.stem('lying') . 词性标注器,pos_tag处理词序列,根据句子动态判断 ...

  6. ansible常用模块入门

    常用模块有以下几个 command copy shell crond yum service setup 1.command模块 ansible george -m command -a " ...

  7. source-insight 常用操作

    [Ctrl + ] 跳转到函数.宏.变量 等定义处. [Alt  > ] 跳转到最近光标停留位置. [Alt  < ] 跳转到上次近光标停留位置. [Ctrl Shift [ ] 块位置开 ...

  8. Git-命令行-删除本地和远程分支

    命令行方式 Git Bash: 切换到要操作的项目文件夹 命令行 : $ cd <ProjectPath> 查看项目的分支们(包括本地和远程) 命令行 : $ git branch -a ...

  9. PySide2兼容PySide1的补丁代码

    Maya2017以及Nuke10的PySide都升级到PySide2了,之前PySide1的大量代码都无法在新软件上使用,这主要是由于PySide2不仅调整了模块位置,还增删了一系列模块,下面我分享一 ...

  10. element-ui 点击获取table的行索引

    <el-table :data="list" v-loading.body="listLoading" element-loading-text=&quo ...