(三) python性能分析-line_profiler模块(时

2019-11-26  本文已影响0人  神经网络爱好者

前言

  在进行模型测试过程中,我们通常需要知道整个预测过程所消耗的时间与空间,以及哪个部分存在瓶颈,才能进行后续的优化。因此,本文介绍我常用的性能分析工具--line_profilermemory_profiler

一、时间分析--line_profiler模块

1.1 安装

$ pip3 install line_profiler

1.2 用法

  line_profiler使用装饰器(@profile)标记需要调试的函数。用kernprof运行代码,被选函数每一行花费的cpu时间以及其他信息就会被记录下来。

# demo.py
@profile
def foo():
    task = []
    for a in range(0, 101):
        for b in range(0, 101):
            if a + b == 100:
                task.append((a, b))
    return task
 
@profile
def run():
    for item in foo():
        pass
 
if __name__ == '__main__':
    run()

运行下面的命令

kernprof -l -v demo.py

-l表示逐行分析,-v用于输出。
同时会输出一个文件:demo.py.lprof,后期可以对.lprof文件进行分析输出结果。

python -m line_profiler demo.py.lprof

1.3 结果分析

Wrote profile results to demo.py.lprof
Timer unit: 1e-06 s

Total time: 0.004891 s
File: demo.py
Function: foo at line 1

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     1                                           @profile
     2                                           def foo():
     3         1          0.0      0.0      0.0      task = []
     4       102         23.0      0.2      0.5      for a in range(0, 101):
     5     10302       2260.0      0.2     46.2          for b in range(0, 101):
     6     10201       2578.0      0.3     52.7              if a + b == 100:
     7       101         30.0      0.3      0.6                  task.append((a, b))
     8         1          0.0      0.0      0.0      return task

Total time: 0.008603 s
File: demo.py
Function: run at line 11

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    11                                           @profile
    12                                           def run():
    13       102       8584.0     84.2     99.8      for item in foo():
    14       101         19.0      0.2      0.2          pass

Timer unit: 1e-06 s:时间单位;
Total time: 0.004891 s:总时间;
Hit:代码运行次数;
%Time:代码占了它所在函数的消耗的时间百分比,通常直接看这一列。

二、内存分析--memory_profiler模块

2.1 安装

  首先安装memory_profiler和psutil(psutil主要用于提高memory_profile的性能,建议安装)(可使用pip直接安装)

$ pip install memory_profiler
$ pip install psutil

2.2 用法

(1)  1.在函数前添加 @profile
    2.运行方式: python -m memory_profiler test.py
    此方法缺点:在调试和实际项目运行时 要增删 @profile 此装饰器

(2)  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 ;
   此方法优点:解决第一种方法的缺点,在不需要分析时,直接注释掉此行。

(3)

脚本代码和方法二一样,但是运行方式不同:
mprof run memory_profiler_test.py   : 分析结果会保存到一个 .dat格式文件中
mprof plot                          : 把结果以图片到方式显示出来(直接在本目录下运行此命令即可,程序会自动找出.dat文件) (要安装  pip install matplotlib)
mprof clean                         : 清空所有 .dat文件

2.3 结果分析

Filename: demo.py

Line #    Mem usage    Increment   Line Contents
================================================
     1   49.160 MiB   49.160 MiB   @profile
     2                             def foo():
     3   49.160 MiB    0.000 MiB       task = []
     4   49.160 MiB    0.000 MiB       for a in range(0, 101):
     5   49.160 MiB    0.000 MiB           for b in range(0, 101):
     6   49.160 MiB    0.000 MiB               if a + b == 100:
     7   49.160 MiB    0.000 MiB                   task.append((a, b))
     8   49.160 MiB    0.000 MiB       return task


Filename: demo.py

Line #    Mem usage    Increment   Line Contents
================================================
    11   49.160 MiB   49.160 MiB   @profile
    12                             def run():
    13   49.160 MiB   49.160 MiB       for item in foo():
    14   49.160 MiB    0.000 MiB           pass

Mem usage : 运行内存大小;
Increment : 运行当前代码后,增加的内存。

上一篇下一篇

猜你喜欢

热点阅读