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 ...
随机推荐
- Spring Boot 框架的依赖管理
Spring Boot为完成不同需求的Spring应用构建,提供了多种不同的依赖管理模板,每种模板均为一系列已完成的依赖的管理.例如在我们的入门程序中,需要构建web项目,我们只需添加spring-b ...
- flask中注册验证码和分页
注册验证码.核心思路,替换注册页面的img标签的src属性. 1.准备好文件夹:captcha2.导包 from utils.captcha.captcha import captcha3.验证码生成 ...
- angular js 初学
1 angularJS的一些概念 AngularJS 通过新的属性和表达式扩展了 HTML. AngularJS 是一个 JavaScript 框架,它是一个以Javascript编写的库,它可通过 ...
- 网络编程三 Socket
1.根据netstat端口的找到进程号---->根据进程号找到进程名称-------->终止进程 1) netstat 最后一列是5432 C:\Users\Administrato ...
- tornado上帝视角第一次建立WEB服务器
import tornado.ioloop import tornado.web 该视角建立在SOCKET服务端和客户端的基础上. class MainHandler(tornado.web.Requ ...
- 冰水挑战 HDU - 6495
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6495 题解:DP!!! dp[i][j] 表示前i个挑战,接受了j个剩余的最大体力,最后输出体力大于0 ...
- python win32com.client
搜集的一些关于win32com.client操作office的相关用法 #创建 #word w = win32com.client.Dispatch("Word.Application&qu ...
- Python基础:三、Python的解释器
当我们编写python代码的时候,我们得到的是一个包含python代码的以.py为拓展名的文本文件,要运行代码,就需要python解释器去执行.py文件. 由于整个python语言从规范到解释器都是开 ...
- api接口的记录
http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1& ...
- Thing in java 第四章,控制执行流程,练习题答案
/** * Created by Sandy.Liu on 2018/7/19. * Thinking in java, version 4, chapter 4, practice 1 * Writ ...