[总结]python逐行性能分析

本文总结了Python性能分析的关键点,包括性能定义,如何使用观察工具如HTTP请求时长来评估,以及如何利用line_profiler这个工具进行代码逐行性能分析,以定位性能瓶颈。在IPython环境中,可以便捷地使用line_profiler装饰器来查看函数的执行次数和时间,从而有效地找出代码中的性能问题。
摘要由CSDN通过智能技术生成

总是忘记,做个记录

性能定义

一般而言,处理一个请求的速度是一个重要的性能指标(Latency, 系统延迟)。

观察工具

有时候不需要特殊工具,直接看一个http请求的时长即可。

定位瓶颈

line_profiler是一个可以查看每行代码的执行次数、执行时间的工具。

安装好后,可以在代码中对方法/函数添加装饰器@profile,然后在命令行使用kernprof -l -v xx.py来查看结果。

这样太麻烦,可以直接在IPython中直接使用。

导入:

In [1]: %load_ext line_profiler

help:

In [2]: %lprun?
Docstring:
Execute a statement under the line-by-line profiler from the
line_profiler module.

Usage:
  %lprun -f func1 -f func2 <statement>

The given statement (which doesn't require quote marks) is run via the
LineProfiler. Profiling is enabled for the functions specified by the -f
options. The statistics will be shown side-by-side with the code through the
pager once the statement has completed.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值