Python性能分析工具pyinstrument提高代码效率
作者:somenzz 发布时间:2021-01-24 02:37:44
天下武功,唯快不破。
编程也不例外,你的代码跑的快,你能快速找出代码慢的原因,你的码功就高。
安装
pip install pyinstrument
简单的使用
在程序的开始,启动 pyinstrument 的 Profiler,结束时关闭 Profiler 并打印分析结果如下:
from pyinstrument import Profiler
profiler = Profiler()
profiler.start()
# 这里是你要分析的代码
profiler.stop()
profiler.print()
比如这段代码 123.py,我们可以清楚的看到是列表推导式比较慢:
from pyinstrument import Profiler
profiler = Profiler()
profiler.start()
# 这里是你要分析的代码
a = [i for i in range(100000)]
b = (i for i in range(100000))
rofiler.stop()
profiler.print()
上述分析需要修改源代码,如果你使用命令行工具,就不需要修改源代码,只需要执行 pyinstrument xxxx.py
即可:
比如有这样一段排序的程序 c_sort.py:
import sys
import time
import numpy as np
arr = np.random.randint(0, 10, 10)
def slow_key(el):
time.sleep(0.01)
return el
arr = list(arr)
for i in range(10):
arr.sort(key=slow_key)
print(arr)
这段代码里面故意放了一句 time.sleep(0.01) 来延迟性能,看看 pyinstrument
能否识别,命令行执行 pyinstrument c_sort.py
:
从结果来看,程序运行了 1.313 秒,而 sleep 就运行了 1.219 秒,很明显是瓶颈,现在我们把它删除,再看看结果:
删除之后,性能最慢的就是 numpy 模块的初始化代码 __init__.py
了,不过这些代码不是自己写的,而且并不是特别慢,就不需要去关心了。
分析 Flask 代码
Web 应用也可以使用这个来找出性能瓶颈,比如 flask,只需要在请求之前记录时间,在请求之后统计时间,只需要在 flask 的请求 * 里面这样写:
from flask import Flask, g, make_response, request
app = Flask(__name__)
@app.before_request
def before_request():
if "profile" in request.args:
g.profiler = Profiler()
g.profiler.start()
@app.after_request
def after_request(response):
if not hasattr(g, "profiler"):
return response
g.profiler.stop()
output_html = g.profiler.output_html()
return make_response(output_html)
假如有这样一个 API:
@app.route("/dosomething")
def do_something():
import requests
requests.get("http://google.com")
return "Google says hello!"
为了测试这个 API 的瓶颈,我们可以在 url 上加一个参数 profile 就可以:http://127.0.0.1:5000/dosomething?profile
,哪一行代码执行比较慢,结果清晰可见:
分析 Django 代码
分析 Django 代码也非常简单,只需要在 Django 的配置文件的 MIDDLEWARE 中添加
"pyinstrument.middleware.ProfilerMiddleware",
然后就可以在 url 上加一个参数 profile 就可以:
如果你不希望所有人都能看到,只希望管理员可以看到,settings.py 可以添加这样的代码:
def custom_show_pyinstrument(request):
return request.user.is_superuser
PYINSTRUMENT_SHOW_CALLBACK = "%s.custom_show_pyinstrument" % __name__
如果不想通过 url 后面加参数的方式查看性能分析,可以在 settings.py 文件中添加:
PYINSTRUMENT_PROFILE_DIR = 'profiles'
这样,每次访问一次 Django 接口,就会将分析结果以 html 文件形式保存在 项目目录下的 profiles 文件夹中。
分析异步代码
简单的异步代码分析:
async_example_simple.py:
import asyncio
from pyinstrument import Profiler
async def main():
p = Profiler()
with p:
print("Hello ...")
await asyncio.sleep(1)
print("... World!")
p.print()
asyncio.run(main())
复杂一些的异步代码分析:
import asyncio
import time
import pyinstrument
def do_nothing():
pass
def busy_wait(duration):
end_time = time.time() + duration
while time.time() < end_time:
do_nothing()
async def say(what, when, profile=False):
if profile:
p = pyinstrument.Profiler()
p.start()
busy_wait(0.1)
sleep_start = time.time()
await asyncio.sleep(when)
print(f"slept for {time.time() - sleep_start:.3f} seconds")
busy_wait(0.1)
print(what)
if profile:
p.stop()
p.print(show_all=True)
loop = asyncio.get_event_loop()
loop.create_task(say("first hello", 2, profile=True))
loop.create_task(say("second hello", 1, profile=True))
loop.create_task(say("third hello", 3, profile=True))
loop.run_forever()
loop.close()
工作原理
Pyinstrument 每 1ms 中断一次程序,并在该点记录整个堆栈。它使用 C 扩展名和 PyEval_SetProfile 来做到这一点,但只每 1 毫秒读取一次读数。你可能觉得报告的样本数量有点少,但别担心,它不会降低准确性。默认间隔 1ms 是记录堆栈帧的下限,但如果在单个函数调用中花费了很长时间,则会在该调用结束时进行记录。如此有效地将这些样本“打包”并在最后记录。
Pyinstrument 是一个统计分析器,并不跟踪,它不会跟踪您的程序进行的每个函数调用。相反,它每 1 毫秒记录一次调用堆栈。与其他分析器相比,统计分析器的开销比跟踪分析器低得多。
比如说,我想弄清楚为什么 Django 中的 Web 请求很慢。如果我使用 cProfile,我可能会得到这个:
151940 function calls (147672 primitive calls) in 1.696 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 1.696 1.696 profile:0(<code object <module> at 0x1053d6a30, file "./manage.py", line 2>)
1 0.001 0.001 1.693 1.693 manage.py:2(<module>)
1 0.000 0.000 1.586 1.586 __init__.py:394(execute_from_command_line)
1 0.000 0.000 1.586 1.586 __init__.py:350(execute)
1 0.000 0.000 1.142 1.142 __init__.py:254(fetch_command)
43 0.013 0.000 1.124 0.026 __init__.py:1(<module>)
388 0.008 0.000 1.062 0.003 re.py:226(_compile)
158 0.005 0.000 1.048 0.007 sre_compile.py:496(compile)
1 0.001 0.001 1.042 1.042 __init__.py:78(get_commands)
153 0.001 0.000 1.036 0.007 re.py:188(compile)
106/102 0.001 0.000 1.030 0.010 __init__.py:52(__getattr__)
1 0.000 0.000 1.029 1.029 __init__.py:31(_setup)
1 0.000 0.000 1.021 1.021 __init__.py:57(_configure_logging)
2 0.002 0.001 1.011 0.505 log.py:1(<module>)
看完是不是还是一脸懵逼,通常很难理解您自己的代码如何与这些跟踪相关联。Pyinstrument 记录整个堆栈,因此跟踪昂贵的调用要容易得多。它还默认隐藏库框架,让您专注于影响性能的应用程序/模块:
_ ._ __/__ _ _ _ _ _/_ Recorded: 14:53:35 Samples: 131
/_//_/// /_\ / //_// / //_'/ // Duration: 3.131 CPU time: 0.195
/ _/ v3.0.0b3
Program: examples/django_example/manage.py runserver --nothreading --noreload
3.131 <module> manage.py:2
└─ 3.118 execute_from_command_line django/core/management/__init__.py:378
[473 frames hidden] django, socketserver, selectors, wsgi...
2.836 select selectors.py:365
0.126 _get_response django/core/handlers/base.py:96
└─ 0.126 hello_world django_example/views.py:4
最后的话
本文分享了 pyinstrument 的用法,有了这个性能分析神器,以后优化代码可以节省很多时间了,这样的效率神器很值得分享,毕竟人生苦短,能多点时间干点有意思的不香么?
来源:https://blog.csdn.net/somenzz/article/details/120192288


猜你喜欢
- #!/usr/bin/env python3# -*- coding: utf-8 -*-# File Name : gt1.py# Pur
- public partial class CMS_DBDataContext { partial void OnCreated() { //
- 本文介绍了两个asp实用的技巧,一是使用asp强制刷新页面,二是判断一个文件是否存在强制刷新网页 强制性刷新随机验证码 ,让随机验
- 锁有两种分类方法。(1) 从数据库系统的角度来看锁分为以下三种类型: •独占锁(Exclusive Lock)独占锁锁定的资源只允许进行锁定
- 目录普通分页查询如何优化偏移量大采用id限定方式优化数据量大问题普通分页查询当我们在日常工作中遇到大数据查询的时候,第一反应就是使用分页查询
- SQL Server中的集合运算包括UNION(合并),EXCEPT(差集)和INTERSECT(相交)三种。集合运算的基本使用1.UNIO
- 创建项目和应用django-admin startproject zqxt_views(项目名)cd zqxt_viewspython ma
- 正在看的ORACLE教程是:Oracle RMAN快速入门指南。前言: 这篇文章主要介绍RMAN的常用方法,其中包含了作者一些自己的经验,里
- Window对象 窗口操作 Window对象对操作浏览器窗口非常有用,开发者可以移动或调整浏览器窗口的大小。可用四种方法实现这些操作: mo
- python继承,python丰富的类因为继承而变得多姿多彩,如果语言不支持继承,那么类就没什么优势。1、首先我们来定义两个类一个dog类,
- 前言最近又多了不少朋友关注,先在这里谢谢大家。关注我的朋友大多数都是大学生,而且我简单看了一下,低年级的大学生居多,大多数都是为了完成课程设
- 前言Redis是一个开源的内存数据库,在项目开发中redis的使用也比较频繁,本文介绍了Go语言中go-redis库的基本使用。感兴趣的小伙
- 本文实例讲述了js比较日期大小的方法。分享给大家供大家参考。具体如下:function DateDiff(d1,d2){ var resul
- 类的代码: define('QR_MODE_NUL', -1); define('QR_MODE_NUM',
- 不想每次都要去查execl,想更方便点,更快一点。通俗点思路:点击exe,Python 自动监控剪贴板的内容,然后正则取出IP,接着根据IP
- 在本文中,我们向您介绍一些提示和技巧,以帮助您更快地编写代码Python的可读性和设计简单性是其广受欢迎的两个主要原因。一些常见的Pytho
- 第一种, 使用create_connection链接,需要pip install websocket-client (此方法不建议使用,链接
- 字段是Python是字典中唯一的键-值类型,是Python中非常重要的数据结构,因其用哈希的方式存储数据,其复杂度为O(1),速度非常快。下
- 出现问题: 1. 使用层制作的下拉菜单下正好有FLASH动画,菜单被动画遮挡. 2. 页面中的层浮动广告当经过FLASH动画时,浮动层从动画
- 今天我想试试能不能用数组来实现矩阵转置呢?想知道,那就接着往下看吧。希望大家读完有所收获,那我辛苦码字也就值了。一、常见二维数组操作🌴创建与