作用: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. Pymongo使用事项

    基本上的教程都有,就不阐述了,这里主要记录使用时遇到的问题 1. 再使用有权限的mongodb连接时,需要user指定role所在的集合,如图: 在pymongo创建MongoClient 除了增加u ...

  2. 新学了几个python模块,不是很鸡肋。

    先说一个模块分类(基本上所有模块都是小写开头,虽然规范的写法是变量的命名规范,但是,都是这样写的) 1,C编写并镶嵌到python解释器中的内置模块 2,包好的一组模块的包 3.已经被编译好的共享库, ...

  3. ML_Clustering

    西瓜书学习...ing K均值 k-means 给定样本集$ D = {X_1,X_2,...X_n} \(,k-means针对聚类所得簇划分\)C = {C_1,C_2,...,C_k}$最小化平方 ...

  4. 【分布式锁】redis实现

    转载:https://www.jianshu.com/p/c970cc710SETNX命令简介 SETNX key value 将key的值设为value,并且仅当key不存在. 若给定的key已经存 ...

  5. 【druid 】数据库连接池

    一.数据库连接池架构 二.数据库连接池的过滤器 spi的思想,加载配置文件的Filter druid.filters.default=com.alibaba.druid.filter.stat.Sta ...

  6. 芯灵思Sinlinx A64 Linux&qt编译安装

    开发平台 芯灵思Sinlinx A64 内存: 1GB 存储: 4GB 详细参数 https://m.tb.cn/h.3wMaSKm 开发板交流群 641395230 前提条件搭建好CentOS环境 ...

  7. vue 项目 使用sass,node-sass 安装方法及cnpm下如何安装node sass

    内容为转载,方便用时查阅,原文地址: https://www.cnblogs.com/crazycode2/p/6535105.html 1,使用save会在package.json中自动添加. 1 ...

  8. 安装netcat(-bash: netcat: command not found)

    安装netcat 在用swoole UDP服务器可以使用netcat -u 来连接测试时,报错-bash: netcat: command not found,是因为centos7未安装netcat. ...

  9. Zabbix 3.4 服务端安装部署

    关于zabbix的安装部署官方也提供了详细的安装文档,链接如下: https://www.zabbix.com/download 选择zabbix的版本,服务器平台及使用的数据库 安装和配置zabbi ...

  10. 为Owin项目增加WebApi

    上一篇文章我们新建了一个Owin项目. 本节,我们来为其增加WebApi功能 项目右键>添加>新搭建基架的项目 选择Web API 2控制器 为默认控制器命名 点击添加后项目结构如下: 将 ...