Python performance analysis tool pyinstrument explanation

I. Introduction

The performance of the program is also a very key indicator. In many cases, your code runs faster, which can better reflect your technology. Recently, it has been found that many small partners use the method of manually printing the running time to count the time-consuming code in the process of performance analysis:

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

output result

0:00:00.377766

This method is very fast to use, but it is a little powerless when it needs to count the execution time of each line of code and generate more complete performance analysis such as visual reports. At this time, a third-party library of python can be used Pyinstrumentfor performance analysis.



2. Use of Pyinstrument

Pyinstrument is a Python analyzer. A profiler is a tool that helps you optimize your code - make it faster. To get the maximum speed boost.
Pyinstrument official documentation: pyinstrument

Pyinstrument installation:

pip install pyinstrument


1. Example

For the example we gave at the beginning, the code implemented using Pyinstrument is as follows:

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

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

output result

  _     ._   __/__   _ _  _  _ _/_   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 #单行代码耗时

The printed information includes information such as record time, number of threads, total time, single-line code time, and CPU execution time.


The effect of use in the case of multi-line code analysis (respectively, it takes time to generate lists of 10 million lengths, 100 million lengths, and 200 million lengths):

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()

output result

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 analyzes django code

Using Pyinstrument to analyze Django code is very simple, just add the following configuration to the Django configuration file :settings.pyMIDDLEWARE

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

Then you can add a parameter profile to the url:
insert image description here


Pyinstrument also supports the performance analysis of flask and asynchronous code. For details, you can check the official documentation for learning.
Pyinstrument also provides a wealth of APIs for us to use, and the official website documentation has a detailed introduction: https://pyinstrument.readthedocs.io/en/latest/reference.html


3. The difference between Pyinstrument and cProfile (python's own performance analyzer)

According to the description of the official documentation, the system overhead of Pyinstrument will be much smaller than that of trace analyzers such as cProfile, which may distort the test results due to a large number of calls to the profiler:
insert image description here




Get learning materials and free Q&A below

Guess you like

Origin blog.csdn.net/momoda118/article/details/122805206