鱼C论坛

 找回密码
 立即注册
查看: 2161|回复: 2

[技术交流] Python 小技巧 046:性能分析(三)—— memory_profiler

[复制链接]
发表于 2020-1-1 17:31:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Python 性能分析 —— memory_profiler


memory_profiler 与 line_profiler 相似,区别是 line_profiler 用于分析程序的用时(时间),而 memory_profiler 用于分析程序的占用内存(空间)。

1. 安装

直接使用 pip 安装即可:

  1. pip install memory_profiler
复制代码


2. 使用

demo.py 文件内容如下:

  1. # -*- coding: utf-8 -*-

  2. from memory_profiler import profile


  3. @profile
  4. def demo():
  5.     a = [1] * 1000
  6.     b = [2] * 1000000
  7.     c = [3] * 500
  8.     c.extend(a)
  9.     a.extend(b)
  10.     b *= 2


  11. demo()
复制代码


切换到 demo.py 所在目录,cmd 执行命令:

  1. python -m memory_profiler demo.py
复制代码


打印了以下内容:

  1. Filename: demo.py

  2. Line #    Mem usage    Increment   Line Contents
  3. ================================================
  4.      6     38.7 MiB     38.7 MiB   @profile
  5.      7                             def demo():
  6.      8     38.7 MiB      0.0 MiB       a = [1] * 1000
  7.      9     46.3 MiB      7.6 MiB       b = [2] * 1000000
  8.     10     46.3 MiB      0.0 MiB       c = [3] * 500
  9.     11     46.3 MiB      0.0 MiB       c.extend(a)
  10.     12     53.9 MiB      7.6 MiB       a.extend(b)
  11.     13     61.6 MiB      7.6 MiB       b *= 2
复制代码


其中:

  • Filename 表示分析的文件名。
  • Line # 表示行号。
  • Mem usage 表示本行运行的内存占用量。
  • Increment 表示当前行的内存相对于上一行增长了多少。
  • Line Content 表示每一行的内容。
  • 1 MiB 约等于 1.05 MB。


3. 参数

  • precision

    precision 用于指定显示内存占用量小数点的位数。默认小数点后显示一位,如果某行代码占用内存比较小,就可能显示不出来。此时可以通过调整小数点后的位数实现。

    demo.py 文件内容如下:

    1. # -*- coding: utf-8 -*-

    2. from memory_profiler import profile


    3. @profile(precision=4)
    4. def demo():
    5.     a = [1] * 1000
    6.     b = [2] * 1000000
    7.     c = [3] * 500
    8.     c.extend(a)
    9.     a.extend(b)
    10.     b *= 2


    11. demo()
    复制代码


    执行结果:

    1. Filename: demo.py

    2. Line #    Mem usage    Increment   Line Contents
    3. ================================================
    4.      6  38.6758 MiB  38.6758 MiB   @profile(precision=4)
    5.      7                             def demo():
    6.      8  38.6758 MiB   0.0000 MiB       a = [1] * 1000
    7.      9  46.3086 MiB   7.6328 MiB       b = [2] * 1000000
    8.     10  46.3086 MiB   0.0000 MiB       c = [3] * 500
    9.     11  46.3086 MiB   0.0000 MiB       c.extend(a)
    10.     12  53.9492 MiB   7.6406 MiB       a.extend(b)
    11.     13  61.5820 MiB   7.6328 MiB       b *= 2
    复制代码

  • stream

    stream 用于指定输出结果的流。每次运行都要打印内存情况,势必会影响程序输出效果,我们可以将结果输出到文件流中。

    demo.py 文件内容如下:

    1. # -*- coding: utf-8 -*-

    2. from memory_profiler import profile


    3. @profile(precision=4, stream=open("E:/demo.txt", "w"))
    4. def demo():
    5.     a = [1] * 1000
    6.     b = [2] * 1000000
    7.     c = [3] * 500
    8.     c.extend(a)
    9.     a.extend(b)
    10.     b *= 2


    11. demo()
    复制代码


    执行后 E:\demo.txt 内容如下:

    1. Filename: demo.py

    2. Line #    Mem usage    Increment   Line Contents
    3. ================================================
    4.      6  38.8672 MiB  38.8672 MiB   @profile(precision=4, stream=open("E:/demo.txt", "w"))
    5.      7                             def demo():
    6.      8  38.8672 MiB   0.0000 MiB       a = [1] * 1000
    7.      9  46.5000 MiB   7.6328 MiB       b = [2] * 1000000
    8.     10  46.5000 MiB   0.0000 MiB       c = [3] * 500
    9.     11  46.5000 MiB   0.0000 MiB       c.extend(a)
    10.     12  54.1406 MiB   7.6406 MiB       a.extend(b)
    11.     13  61.7734 MiB   7.6328 MiB       b *= 2
    复制代码


4. mprof 命令

memory_profiler 自带 mprof 命令。

  1. Usage: mprof <command> <options> <arguments>

  2. Available commands:

  3.     run      运行给定的命令或 python 文件
  4.     rm       删除 mprof 生成的给定文件
  5.     clean    清除当前目录中 mprof 创建的文件
  6.     list     显示带索引的现有配置文件
  7.     plot     可以将 mprof run 的结果生成图片

  8. Type mprof <command> --help for usage help on a specific command.
  9. For example, mprof plot --help will list all plotting options.
复制代码


说说 plot 参数。run 命令可以生成 .dat 文件,plot 可以将根据该文件生成图片,不过需要安装 matplotlib 包。

例子:

  1. E:\Programming\Python\项目>mprof run demo.py
  2. mprof: Sampling memory every 0.1s
  3. running as a Python program...
  4. Filename: demo.py

  5. Line #    Mem usage    Increment   Line Contents
  6. ================================================
  7.      6  38.6836 MiB  38.6836 MiB   @profile(precision=4)
  8.      7                             def demo():
  9.      8  38.6836 MiB   0.0000 MiB       a = [1] * 1000
  10.      9  46.3164 MiB   7.6328 MiB       b = [2] * 1000000
  11.     10  46.3164 MiB   0.0000 MiB       c = [3] * 500
  12.     11  46.3164 MiB   0.0000 MiB       c.extend(a)
  13.     12  53.9570 MiB   7.6406 MiB       a.extend(b)
  14.     13  61.5898 MiB   7.6328 MiB       b *= 2



  15. E:\Programming\Python\项目>mprof list
  16. 0 mprofile_20200101172926.dat 17:29:26 01/01/2020

  17. E:\Programming\Python\项目>mprof plot mprofile_20200101172926.dat
复制代码


图表:

1.png

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-1-1 19:16:31 From FishC Mobile | 显示全部楼层
沙发!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-12 18:29:07 | 显示全部楼层
(又又是我)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-26 10:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表