Python中性能分析利器pyinstrument详细讲解

作者:曲鸟 时间:2021-02-15 10:46:51 

一、前言

程序的性能也是非常关键的指标,很多时候你的代码跑的快,更能够体现你的技术。最近发现很多小伙伴在性能分析的过程中都是手动打印运行时间的方式来统计代码耗时的:

import datetime
start=datetime.datetime.now()
b=[i for i in range(10000000)]  # 生成长度为一千万的列表
end=datetime.datetime.now()
print(end-start)

输出结果

0:00:00.377766

这种方法使用很快捷,但需要统计每行代码的执行时间,生成可视化的报告等更完善的性能分析时就有点力不从心了。这个时候可以使用python的第三方库Pyinstrument来进行性能分析。

二、Pyinstrument使用

Pyinstrument 是一个 Python 分析器。分析器是一种帮助您优化代码的工具 - 使其更快。要获得最大的速度提升。
Pyinstrument 官方文档:pyinstrument

Pyinstrument 安装:

pip install pyinstrument

1. 举例

对于最开始我们举的例子,使用Pyinstrument实现的代码如下:

文末添加个人VX,获取资料和免费答疑

from pyinstrument import Profiler
profiler=Profiler()
profiler.start()
b=[i for i in range(10000000)]# 生成长度为一千万的列表
profiler.stop()
profiler.print()

输出结果

  _     ._   __/__   _ _  _  _ _/_   Recorded: 10:39:54  Samples:  1
 /_//_/// /_\ / //_// / //_'/ //     Duration: 0.385     CPU time: 0.391
/   _/                      v4.1.1
Program: D:/code/server/aitestdemo/test2.py
0.385 <module>  test2.py:2  #执行总耗时
└─ 0.385 <listcomp>  test2.py:7 #单行代码耗时

打印的信息包含了记录时间、线程数、总耗时、单行代码耗时、CPU执行时间等信息。

在多行代码分析的情况下的使用效果(分别统计生成一千万长度、一亿长度、两亿长度的列表耗时):

from pyinstrument import Profiler

profiler = Profiler()
profiler.start()
a = [i for i in range(10000000)]  # 生成长度为一千万的列表
b = [i for i in range(100000000)]  # 生成长度为一亿的列表
c = [i for i in range(200000000)]  # 生成长度为十亿的列表
profiler.stop()
profiler.print()

输出结果

Program: D:/code/server/aitestdemo/test2.py
16.686 <module>  test2.py:1
├─ 12.178 <listcomp>  test2.py:9
├─ 4.147 <listcomp>  test2.py:8
└─ 0.358 <listcomp>  test2.py:7

2. Pyinstrument分析django代码

使用Pyinstrument分析 Django 代码非常简单,只需要在 Django 的配置文件settings.pyMIDDLEWARE 中添加如下配置:

MIDDLEWARE = [
...
   'pyinstrument.middleware.ProfilerMiddleware',
...
]

然后就可以在 url 上加一个参数 profile 就可以:

Python中性能分析利器pyinstrument详细讲解

Pyinstrument还支持flask、异步代码的性能分析,具体可以查看官方文档进行学习。
Pyinstrument也提供了丰富的api供我们使用,官网文档有详细的介绍:https://pyinstrument.readthedocs.io/en/latest/reference.html

三、Pyinstrument与cProfile(python自带性能分析器)的不同

根据官方文档的描述来看,Pyinstrument的系统开销会比cProfile 这类跟踪分析器小很多,cProfile由于大量调用探查器,可能会扭曲测试结果:

Python中性能分析利器pyinstrument详细讲解

来源:https://blog.csdn.net/momoda118/article/details/122805206

标签:Python,pyinstrument
0
投稿

猜你喜欢

  • py3nvml实现GPU相关信息读取的案例分析

    2022-01-11 11:00:52
  • python Django 反向访问器的外键冲突解决

    2022-05-19 23:30:40
  • Python爬虫基础讲解之scrapy框架

    2023-11-16 00:07:31
  • 使用JScript遍历Request表单参数集合

    2011-02-26 11:08:00
  • sqlserver、mysql获取连接字符串步骤

    2024-01-22 00:49:33
  • CSS阴影详解

    2009-12-04 18:31:00
  • 50种方法巧妙优化SQL Server数据库

    2008-12-24 15:49:00
  • 查看Django和flask版本的方法

    2021-01-29 02:50:11
  • 关于Django显示时间你应该知道的一些问题

    2023-10-23 06:26:21
  • vue中的ref和$refs的使用

    2024-05-13 09:08:44
  • python logging日志模块的详解

    2021-04-27 19:16:55
  • sql语句中临时表使用实例详解

    2024-01-15 22:39:11
  • python实操练习案例(一)

    2022-01-18 12:10:41
  • Python获取图像中像素点坐标实例代码

    2021-02-20 19:42:05
  • javascript二维数组转置实例

    2023-08-25 07:11:14
  • Python编程实现数学运算求一元二次方程的实根算法示例

    2023-08-08 05:38:40
  • JavaScrip如果基于url实现图片下载

    2023-08-24 22:31:41
  • python实现KNN分类算法

    2023-03-01 07:53:36
  • 二级联动下拉菜单javascript源码

    2010-03-16 12:32:00
  • 在数据库里将毫秒转换成date格式的方法

    2024-01-19 01:27:00
  • asp之家 网络编程 m.aspxhome.com