惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
U
Unit 42
GbyAI
GbyAI
M
MIT News - Artificial intelligence
美团技术团队
罗磊的独立博客
雷峰网
雷峰网
量子位
博客园 - 【当耐特】
Last Week in AI
Last Week in AI
D
Docker
小众软件
小众软件
S
SegmentFault 最新的问题
Blog — PlanetScale
Blog — PlanetScale
阮一峰的网络日志
阮一峰的网络日志
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog
WordPress大学
WordPress大学
V
V2EX
博客园_首页
腾讯CDC
The Cloudflare Blog
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
How to Profile Your Python Script/Module
2023-09-13 · via jdhao's digital space

In my previous post, I shared how to profile a single function using line_profiler. In this post, I want to share how to profile Python script/module and visualize the profile stat to pinpoint the slow part of the code.

Generate the raw profiling stat#

In Python, we can use cProfile to generate the raw profile result:

python -m cProfile -o {script_name}.prof {your_script}.py [script_argument]

The -o options tells cProfile to generate a profile stat file, which is in binary format and not human readable. In order to interpret the profiling stat easily, we need to install some tools to visualize the result.

static profile graph generation#

flameprof#

flameprof can generate a flame graph in SVG format. It is not actively maintained anymore.

pip install flameprof
flameprof input.prof > output.svg

gprof2dot#

gprof2dot can generate a directed call graph for your script with their running time.

pip install gprof2dot
gprof2dot -f pstats input.prof | dot -Tsvg -o output.svg

interactive profile graph generation#

snakeviz#

snakeviz can also generate flamegraph from cProfile stat. It also has great interactive UI to explore the profiling result.

pip install snakeviz
snakeviz input.prof

tuna#

Tuna is like snakeviz, but it claims to solve some of the limitation of snakeviz. The UI is less polished compared to snakeviz, though.

Py-spy#

py-spy can work with speedscope to generate flamegraph.

pip install py-spy
# on macOS, needs sudo to run py-spy
sudo py-spy record --format speedscope -r 500 -F -o output.speedscope python some_script.py
chown -R {your-user-name} output.speedscope
# use speedscope to open the file, https://www.speedscope.app/

py-spy can also profile the running program without interrupting it.

pyinstrument#

pyinstrument is another tool you can use to generate profiling graph. You can directly show the result in terminal:

pip install pyinstrument
pyinstrument your_script.py

You can also generate other output format:

pyinstrument -r html your_script.py

# or generate speedscope file, then inspect with speedscope
pyinstrument -r speedscope your_script.py

references#