• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python性能分析工具pyinstrument提高代码效率

python 搞代码 4年前 (2022-01-09) 19次浏览 已收录 0个评论
文章目录[隐藏]

天下武功,唯快不破。

编程也不例外,你的代码跑的快,你能快速找出代码慢的原因,你的码功就高。

安装

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__<i style="color:transparent">本文来源gaodai$ma#com搞$代*码*网(</i>)
@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!"

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python性能分析工具pyinstrument提高代码效率

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址