V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
zealinux
V2EX  ›  Python

Python 有没有好用的性能优化 Profiler 工具?

  •  
  •   zealinux · 2022-04-07 22:33:28 +08:00 · 2591 次点击
    这是一个创建于 721 天前的主题,其中的信息可能已经有所发展或是发生改变。
    程序跑得慢,想看看瓶颈在哪里,

    我试过了下面几个(应该常用的吧),都没有中意的

    - cProfile
    - vprof
    - pyinstrument
    - py-spy


    大家有何推荐?
    9 条回复    2022-04-28 09:00:50 +08:00
    zhoudaiyu
        1
    zhoudaiyu  
       2022-04-08 00:56:44 +08:00 via iPhone
    py-spy 挺好用的啊,有啥痛点
    zealinux
        2
    zealinux  
    OP
       2022-04-08 09:34:15 +08:00
    @zhoudaiyu 我用了它,但没有火焰图出来
    windvans
        3
    windvans  
       2022-04-08 10:31:41 +08:00
    scalene
    ruanimal
        4
    ruanimal  
       2022-04-08 15:14:17 +08:00
    简单用就 line_profile, 复杂点就是用工具生成火焰图再分析吧
    wcsjtu
        5
    wcsjtu  
       2022-04-11 14:27:38 +08:00
    @windvans 老哥,scalene 怎么针对特定的函数作 profile 啊。 我看它是统计了整个文件的运行时间,连全局的 import 语句的时间也会被统计。 而这些 import 很有可能是在加载模型, 时间会很长。。。。。。

    比如说, 下面的代码

    ```python
    import somelib # 加载模型
    # ...... 省略部分代码
    somelib.process(*args) # 执行推理。 有没有办法只针对这一句做 profile 的
    ```
    ebingtel
        6
    ebingtel  
       2022-04-13 09:12:26 +08:00
    @zealinux 用法不对吧?
    encro
        7
    encro  
       2022-04-14 17:13:51 +08:00
    没记错的话,某个可以直接在需要的函数上加一个装饰器就可以了。
    encro
        8
    encro  
       2022-04-14 17:20:10 +08:00
    windvans
        9
    windvans  
       2022-04-28 09:00:50 +08:00
    @wcsjtu @encro 的说法是正确的,貌似文档里面有个功能可以针对某一个代码块或者函数进行 profile
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5741 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 06:25 · PVG 14:25 · LAX 23:25 · JFK 02:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.