导读

相信日常使用Python作为生产力的读者,一定会存在想要分析代码中每一行的运行时间与变量占用内存大小的需求,本文主要分析两个模块,用于分析每行代码的内存使用情况和运行时间情况。

内存使用

  • memory-profiler [1]

安装

pip install memory-profiler

使用方法一

  1. 在需要分析的函数上,添加装饰器@profile
@profiledef test1(): c=0 for item in xrange(100000):   c+=1  print (c)

  1. 使用下面的命令运行
python -m memory_profiler memory_profiler_test.py     

使用方法二

 from memory_profiler import profile

 @profile(precision=4,stream=open('memory_profiler.log','w+'))# @profiledef test1():     c=0     for item in xrange(100000):         c+=1     print c

# 直接运行即可    

结果

Filename: memory_profiler_test.pyLine #    Mem usage    Increment   Line Contents
================================================5   21.492 MiB   21.492 MiB   @profile6                             def test1():7   21.492 MiB    0.000 MiB       c=08   21.492 MiB    0.000 MiB       for item in xrange(100000):9   21.492 MiB    0.000 MiB           c+=110   21.492 MiB    0.000 MiB       print c
  • Mem usage: 内存占用情况
  • Increment: 执行该行代码后新增的内存

运行时间

  • line-profiler [2]

安装

pip install line-profiler

使用

  1. 在需要分析的函数上,添加装饰器@profile
@profiledef slow_function(a, b, c):    ...
  1. 运行
python -m line_profiler script_to_profile.py.lprof

结果

Pystone(1.1) time for 50000 passes = 2.48This machine benchmarks at 20161.3 pystones/secondWrote profile results to pystone.py.lprofTimer unit: 1e-06 s

File: pystone.pyFunction: Proc2 at line 149Total time: 0.606656 s

Line #      Hits         Time  Per Hit   % Time  Line Contents==============================================================   149                                           @profile   150                                           def Proc2(IntParIO):   151     50000        82003      1.6     13.5      IntLoc = IntParIO + 10   152     50000        63162      1.3     10.4      while 1:   153     50000        69065      1.4     11.4          if Char1Glob == 'A':   154     50000        66354      1.3     10.9              IntLoc = IntLoc - 1   155     50000        67263      1.3     11.1              IntParIO = IntLoc - IntGlob   156     50000        65494      1.3     10.8              EnumLoc = Ident1   157     50000        68001      1.4     11.2          if EnumLoc == Ident1:   158     50000        63739      1.3     10.5              break   159     50000        61575      1.2     10.1      return IntParIO
  • 每列含义
> - Line #: The line number in the file.> - Hits: The number of times that line was executed.> - Time: The total amount of time spent executing the line in the timer's units. In the header information before the tables, you will see a line "Timer unit:" giving the conversion factor to seconds. It may be different on different systems.> - Per Hit: The average amount of time spent executing the line once in the timer's units.> - % Time: The percentage of time spent on that line relative to the total amount of recorded time spent in the function.> - Line Contents: The actual source code. Note that this is always read from disk when the formatted results are viewed, *not* when the code was executed. If you have edited the file in the meantime, the lines will not match up, and the formatter may not even be able to locate the function for display.

欢迎Star -> 学习目录

更多教程 -> 学习目录


参考资料

[1]

memory-profiler: https://pypi.org/project/memory-profiler/

[2]

line-profiler: https://github.com/rkern/line_profiler

本文由 mdnice 多平台发布

Python日学壹技:性能分析相关推荐

  1. 日学壹技:json.load() vs json.loads()

    导读 本文[1]演示如何使用 Python 的 json.load() 和 json.loads() 方法从文件和字符串中读取 JSON 数据.使用 json.load() 和 json.loads( ...

  2. 8.7 python 日学 线程进阶、协程

    1.同步 例如8.6上一篇博客最后的那个问题,之所以会出现那样的情况原因就是,没有控制多个线程对同一资源的访问,对数据造成破坏,使得线程的结果不可预期,这样的现象称为'线程不安全' 解决思路:利用同步 ...

  3. Python 优化第一步: 性能分析实践 使用cporfile+gprof2dot可视化

    拿来主义: python -m cProfile -o profile.pstats to_profile.py gprof2dot -f pstats profile.pstats |dot -Tp ...

  4. Python MySQLdb 循环插入execute与批量插入executemany性能分析(list批量写法亲测成功)

    用Python连接MySQL数据库时,会用到MySQLdb库,这里下载↓↓↓ https://pypi.python.org/pypi/MySQL-python/ 这个库提供了对数据库的普遍操作,增删 ...

  5. 以爬取知乎为例,进行python 多进程爬虫性能分析

    以爬取知乎为例,进行python 多进程爬虫性能分析 如果对多进程multiproessing模块不熟悉,请先浏览 python 使用multiprocessing模块进行多进程爬虫 问题背景: 爬取 ...

  6. Python性能分析 (Profiling)

    此页由Linux Wiki用户 Chenxing于2012年3月21日 (星期三) 07:02的最后更改. 提示:此文已超过 2 年(960 天)未更新,如发现内容过时或有误,欢迎改进:) 性能分析( ...

  7. python调用c优缺点_Python调用C模块以及性能分析

    一.c,ctypes和python的数据类型的对应关系 ctypes type ctype Python type c_char char 1-character string c_wchar wch ...

  8. 算法与数据结构(part2)--Python内置类型性能分析

    学习笔记,仅供参考 文章目录 算法与数据结构--基于python Python内置类型性能分析 timeit模块 计时器类timeit.Timer 计时器类下的timeit.Timer.timeit方 ...

  9. python性能分析工具模块_python——关于Python Profilers性能分析器

    1. 介绍性能分析器 profiler是一个程序,用来描述运行时的程序性能,并且从不同方面提供统计数据加以表述.Python中含有3个模块提供这样的功能,分别是cProfile, profile和ps ...

最新文章

  1. 创新工场有哪些失败项目?不要只看着成功
  2. 10个你值得收藏的牛逼开源后台控制面板
  3. 12.4日团队工作总结
  4. 前端学习(1435):vue能做什么
  5. jdbctemplate无where条件查询_多表查询
  6. 【英语学习】【医学】无机化学 - 化合物命名(3) - 含氧酸/无氧酸
  7. HDU - 2602 01背包
  8. wifi的country code
  9. 编程软件哪个比较好用?
  10. Linux 内核参数:meminfo
  11. labview打包文档_labview怎么生成exe文件
  12. 成分句法分析综述(第二版)
  13. 实时的软件生成 —— Prompt 编程打通低代码的最后一公里?
  14. 清华学霸讲计算机,清华学霸的霸气演讲!看完后才明白人与人的差距就是这样拉开的!...
  15. 素描原理在PS鼠绘实物中的应用
  16. 「软件合集」免费分享15款小众实用软件,没多少人知道,打包送你
  17. Windows 家族吐槽大会
  18. 准到吓人的手相,教你看手相掌握将来
  19. 家电行业APS如何选型?
  20. Pixelmator Pro 2.0:预设滤镜和超 200 款预设样式重磅登场

热门文章

  1. Centos下堡垒机Jumpserver V3.0环境部署完整记录(1)-安装篇
  2. 校招面试 - 计算机网络 - (非)对称加密 - 安全传输的基础
  3. 店铺如何快速实现数字化管理?不妨参考一下管理系统
  4. 茶叶包装盒产品如何做好软文营销 利用软文来打造为产品引流宣传
  5. 【网络】交换机的原理和配置方法
  6. 微软、百度、腾讯、阿里、蚂蚁金服:实习面经
  7. matlab 比较两个结构体,用于比较 MATLAB 结构体数组的比较器 - MATLAB - MathWorks 中国...
  8. Ichunqiu云境 —— Exchange Writeup
  9. SpringCloud版本
  10. 光伏电站监控系统实时监控保障安全运维