博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
memory_profiler的使用
阅读量:7036 次
发布时间:2019-06-28

本文共 1762 字,大约阅读时间需要 5 分钟。

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

使用方法一:

   1.在函数前添加 @profile

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

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

代码如下:

1 #coding:utf8 2  3 @profile 4 def test1(): 5     c=0 6     for item in xrange(100000): 7         c+=1 8     print c 9 10 if __name__=='__main__':11     test1()

输出结果:

rgc@rgc:~/baidu_eye/carrier/test$ python -m memory_profiler memory_profiler_test.py 100000Filename: memory_profiler_test.pyLine #    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

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

1 #coding:utf8 2 from memory_profiler import profile 3  4 @profile(precision=4,stream=open('memory_profiler.log','w+')) 5 # @profile 6 def test1(): 7     c=0 8     for item in xrange(100000): 9         c+=110     print c11 12 if __name__=='__main__':13     test1()

 

使用方法三:

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

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

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

       mprof clean                                           : 清空所有 .dat文件

 

转载于:https://www.cnblogs.com/rgcLOVEyaya/p/RGC_LOVE_YAYA_603days_1.html

你可能感兴趣的文章
iOS CoreData介绍和使用(以及一些注意事项)
查看>>
项目管理——任务分配闲谈
查看>>
MySQL数据库设置远程访问权限方法小结
查看>>
PS使用技巧
查看>>
【Linux】 CentOS7 虚拟机配置
查看>>
打造属于自己的支持版本迭代的Asp.Net Web Api Route
查看>>
HttpWebResponse类
查看>>
从Lua调用C
查看>>
Vue+Webpack常见问题(持续更新)
查看>>
機器學習基石(Machine Learning Foundations) 机器学习基石 作业四 Q13-20 MATLAB实现
查看>>
《C预处理》Linux内核中可变参数宏的用法
查看>>
DEDECMS 留言薄模块的使用方法
查看>>
Android Unknown failure (Failure - not installed for 0)
查看>>
域名解析举例
查看>>
用微软云,激活万物互联的“智能”
查看>>
[转] Dangers of using dlsym() with RTLD_NEXT
查看>>
具体解说Android的图片下载框架UniversialImageLoader之磁盘缓存(一)
查看>>
【新手总结】在.Net项目中使用Redis作为缓存服务
查看>>
[转]微服务概念解析
查看>>
DP的四边形优化
查看>>