memory_profiler的使用
作用: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的使用的更多相关文章
- psutil 是因为该包能提升 memory_profiler 的性能
		
python 性能分析入门指南 一点号数据玩家昨天 限时干货下载:添加微信公众号"数据玩家「fbigdata」" 回复[7]免费获取[完整数据分析资料!(包括SPSS.SAS.SQ ...
 - python 内存监控模块之memory_profiler
		
0. memory_profiler是干嘛的 This is a python module for monitoring memory consumption of a process as wel ...
 - Python的7种性能测试工具:timeit、profile、cProfile、line_profiler、memory_profiler、PyCharm图形化性能测试工具、objgraph
		
1.timeit: >>> import timeit >>> def fun(): ): a = i * i >>> timeit.timeit ...
 - python--cProfile,memory_profiler,psutil
		
关于测试代码用了多长时间,我们之前介绍了timeit.相较于timeit,python中还有一个更加强大的模块,cProfile模块 (提到cProfile,其实还有一个profile,但profil ...
 - python的memory_profiler模块使用
		
本文主要介绍了python内存分析工具: memory_profiler,可以展示每一行代码执行所增加的内存,方便做内存调优和排除bug memory_profiler是第三方模块,需要安装才能使用 ...
 - 使用memory_profiler异常
		
在使用memory_profiler模块0.55.0版本执行命令诊断程序内存用量时,遇到下面错误: C:\Users\Chen\Desktop\python_doc\第四模块课件>python ...
 - python性能检测工具整理
		
python 运行后出现core dump产生core.**文件,可通过gdb来调试 Using GDB with a core dump having found build/python/core ...
 - python 检查内存
		
################################# 测试函数运行内存# coding=utf-8# pip install memory_profiler# pip install p ...
 - 转帖:Python应用性能分析指南
		
原文:A guide to analyzing Python performance While it’s not always the case that every Python program ...
 
随机推荐
- 常用的JSP内置对象(1)
			
常用的JSP内置对象 request对象主要用于处理客户端请求 request对象的作用是与客户端交互,收集客户端的Form.Cookies.超链接,或者收集服务器端的环境变量. request对象常 ...
 - Python之PIL库的运用、GIF处理
			
一.PIL库简介 PIL(Python Image Library)库是Python语言的第三方库,它支持图像存储.显示和处理,它能够处理几乎所有图片格式,可以完成对图像的缩放.剪裁.折叠以及像图片添 ...
 - 第五章JavaScript
			
创建数组://1.字面量方式创建 (推荐大家使用这种方式创建数组 简单粗暴) var colors = ['red','color','yellow'];console.log(colors) //空 ...
 - Linux----------mysql基础
			
目录 一.数据库介绍 1.1 数据库的优点 1.2 数据库的基本功能 1.3数据库的类型 1.4 关系型数据的组成 1.5 关系型数据库的常用组件 1.6 SQL语句 1.7 mysql命令使用 1. ...
 - opendistro 试用
			
以前转载过一篇别人的关于opendistro的文章,还好使用docker-compose 运行,很方便,所以自己也跑下 环境准备 docker-compose 文件 version: '3' serv ...
 - java8_api_字符串处理
			
字符串处理1 字符串处理2 string的常用方法 package java_20180209_api_string; public class StringDemo2 { public sta ...
 - 手写SpringMVC实现
			
设计自己的MVC架构 1.读取配置通过web.xml中加载我们自己写的MyDispatcherServlet和读取配置文件. 2.初始化九大组件只需实现基本的:1).加载配置文件:2).扫描用户配置包 ...
 - IPv6学习笔记
			
IPv6简写规范: 1) 每个IPv6地址段起始的0可以被省略: 2) 如果一段为4个零,可以简写为一个0 3) 如果有连续的多个段全为0,则可以使用::表示 注:一个地址段中只能有一个::出现 ...
 - http摘要认证
			
摘要认证步骤: 1. 客户端访问一个受http摘要认证保护的资源. 2. 服务器返回401状态以及nonce等信息,要求客户端进行认证. HTTP/1.1 401 Unauthor ...
 - 如何使用linux+xvfb+python+rfs+firefox+jenkins实现UI自动化
			
首先说一下背景,在项目中使用windows执行机进行rfs脚本运行时,遇到两个问题: 1.Jenkins的多个slave节点和master的连接通过windows插件去建立,如果出现slave断连,无 ...