作用: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. xterm配置

    最近转到i3wm桌面下, 发现调用xfce4-terminal有些慢,索性卸载掉一切所谓高级的终端,使用xterm,其实这个才是更牛的家伙. 安装 apt install xterm in i3-wm ...

  2. Linux下tomcat运行时jvm内存分配

    tomcat运行时jvm内存分配 ⑴开发环境下在myeclipse中配置-Xms256m -Xmx512m -XX:NewSize=64m -XX:MaxNewSize=128m -XX:PermSi ...

  3. JavaScript如何让1+1=11;{ } + { } = 2

    delete (          ) delete (          ) ;var  n = new Number( 1 ) console.log( n + 1 )   // 2  #请在括号 ...

  4. 2018-2019-2 20165313 Exp3 免杀原理与实践

    实践内容(3.5分) 1.1 正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion,自己利用shellcode编程等免杀工具或技巧:(1.5分) 1.2 通过组合 ...

  5. explain和profiling分析查询SQL时间

    mysql可以通过profiling命令查看到执行查询SQL消耗的时间. 默认情况下,mysql是关闭profiling的,命令: select @@profiling; +------------- ...

  6. Evosuite使用方法入门

    ​ Evosuite使用方法入门 ​ 1.简要介绍 EvoSuite开源工具可以基于Eclipse进行测试用例的自动生成,生成的测试用例符合Junit标准(直接生成可进行Junit的java文件),满 ...

  7. Servlet接口UML图

    Servlet.class源码

  8. JVM-crash查看hs_err_pid.log日志

    参考链接: https://www.cnblogs.com/shiyangxt/archive/2009/01/06/1370627.html https://blog.csdn.net/chenss ...

  9. 嵌入式C语言预处理使用

    #include  包含头文件 #define 宏 #define 宏名 (宏体)    //不进行语法检查 #define ABC(x) (5+(x))    //宏函数 #define #else ...

  10. Python 模块源

    1.官方:https://pypi.org/ 2.LFD UCI :https://www.lfd.uci.edu/~gohlke/pythonlibs/ 4.清华源:https://pypi.tun ...