python性能分析之line_profiler模块
line_profiler使用装饰器(@profile)标记需要调试的函数.用kernprof.py脚本运行代码,被选函数每一行花费的cpu时间以及其他信息就会被记录下来。
安装
pip3 install Cpython
pip3 install Cython git+https://github.com/rkern/line_profiler.git
代码演示
loopdemo.py 100以内哪两个数相加等于100.
首先是没有优化过的双层循环的嵌套
@profile
def foo():
task = []
for a in range(0, 101):
for b in range(0, 101):
if a + b == 100:
task.append((a, b))
return task
@profile
def run():
for item in foo():
pass
if __name__ == '__main__':
run()
运行下面的命令
kernprof -l -v loopdemo.py
-l表示逐行分析,-v用于输出。同时会输出一个文件:juliademo.py.lprof,后期可以对.lprof文件进行分析
输出结果
Wrote profile results to loopdemo.py.lprof
Timer unit: 1e-06 s
Total time: 0.009856 s
File: loopdemo.py
Function: foo at line 1
Line # Hits Time Per Hit % Time Line Contents
==============================================================
1 @profile
2 def foo():
3 1 1.0 1.0 0.0 task = []
4
5 102 47.0 0.5 0.5 for a in range(0, 101):
6 10302 4741.0 0.5 48.1 for b in range(0, 101):
7 10201 4975.0 0.5 50.5 if a + b == 100:
8 101 91.0 0.9 0.9 task.append((a, b))
9 1 1.0 1.0 0.0 return task
Total time: 0.017778 s
File: loopdemo.py
Function: run at line 12
Line # Hits Time Per Hit % Time Line Contents
==============================================================
12 @profile
13 def run():
14 102 17747.0 174.0 99.8 for item in foo():
15 101 31.0 0.3 0.2 pass
引入kernprof.py会额外的增加是时间,但是为了检测代码每一行发生了什么,这个影响没什么,实际代码运行的时候是不带@profile装饰器的只有需要line_profiler进行逐行分析的时候才需要加。
总用时Total time: 0.017778 s
Hits是调用次数。
%Time 列告诉我们哪行代码占了它所在函数的消耗的时间百分比,可以看出在foo函数中最消耗时间的是判断a+b==100,占用了50.5%的时间。
然后我对循环部分做下面的优化其他地方不变。
for a in range(0, 101):
b = 100 - a
task.append((a, b))
return task
得到下面的结果
Wrote profile results to loopdemo.py.lprof
Timer unit: 1e-06 s
Total time: 0.000126 s
File: loopdemo.py
Function: foo at line 1
Line # Hits Time Per Hit % Time Line Contents
==============================================================
1 @profile
2 def foo():
3 1 1.0 1.0 0.8 task = []
4
5 102 33.0 0.3 26.2 for a in range(0, 101):
6 101 47.0 0.5 37.3 b = 100 - a
7 101 45.0 0.4 35.7 task.append((a, b))
8 1 0.0 0.0 0.0 return task
Total time: 0.000282 s
File: loopdemo.py
Function: run at line 11
Line # Hits Time Per Hit % Time Line Contents
==============================================================
11 @profile
12 def run():
13 102 256.0 2.5 90.8 for item in foo():
14 101 26.0 0.3 9.2 pass
可以发现总用时,循环体里代码的调用次数减少了
python性能分析之line_profiler模块的更多相关文章
- python性能分析之cProfile模块
cProfile是标准库内建的分析工具的其中一个,另外两个是hotshot和profile -s cumulative -s cumulative开关告诉cProfile对每个函数累计花费的时间进行排 ...
- 如何进行 Python性能分析,你才能如鱼得水?
[编者按]本文作者为 Bryan Helmig,主要介绍 Python 应用性能分析的三种进阶方案.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 我们应该忽略一些微小的效率提升,几乎在 9 ...
- Python性能分析
Python性能分析 https://www.cnblogs.com/lrysjtu/p/5651816.html https://www.cnblogs.com/cbscan/articles/33 ...
- python性能分析(一)——使用timeit给你的程序打个表吧
前言 我们可以通过查看程序核心算法的代码,得知核心算法的渐进上界或者下界,从而大概估计出程序在运行时的效率,但是这并不够直观,也不一定十分靠谱(在整体程序中仍有一些不可忽略的运行细节在估计时被忽略了) ...
- Python性能分析工具Profile
Python性能分析工具Profile 代码优化的前提是需要了解性能瓶颈在什么地方,程序运行的主要时间是消耗在哪里,对于比较复杂的代码可以借助一些工具来定位,python 内置了丰富的性能分析工具,如 ...
- Python性能分析与优化PDF高清完整版免费下载|百度云盘
百度云盘|Python性能分析与优化PDF高清完整版免费下载 提取码:ubjt 内容简介 全面掌握Python代码性能分析和优化方法,消除性能瓶颈,迅速改善程序性能! 对于Python程序员来说,仅仅 ...
- Python丨Python 性能分析大全
虽然运行速度慢是 Python 与生俱来的特点,大多数时候我们用 Python 就意味着放弃对性能的追求.但是,就算是用纯 Python 完成同一个任务,老手写出来的代码可能会比菜鸟写的代码块几倍,甚 ...
- Python—— 性能分析入门指南
虽然并非你编写的每个 Python 程序都要求一个严格的性能分析,但是让人放心的是,当问题发生的时候,Python 生态圈有各种各样的工具可以处理这类问题. 分析程序的性能可以归结为回答四个基本问题: ...
- Python 性能分析工具简介
Table of Contents 1. 性能分析和调优工具简介 1.1. Context Manager 1.2. Decorator 1.3. 系统自带的time命令 1.4. python ti ...
随机推荐
- Ansible Callback
非api模式下自定义callback ansible.cfg中开启callback功能 callback_plugins = /usr/share/ansible/plugins/callback # ...
- springboot入门使用
一.什么是springboot,有什么用 springboot是一个开发框架,其出现的目的利用约定大于配置的思想来让开发者摆脱spring繁琐的配置,简化开发.其不是spring框架的替代品,是spr ...
- Timus 1132 Square Root(二次剩余 解法2)
不理解,背板子 #include<cstdio> using namespace std; int Pow(int a,int b,int p) { ; ) ) res=1LL*a*res ...
- Spark源码解析 - Spark-shell浅析
1.准备工作 1.1 安装spark,并配置spark-env.sh 使用spark-shell前需要安装spark,详情可以参考http://www.cnblogs.com/swordfall/p/ ...
- 如何在Mac上搭建自己的服务器——Nginx
1.安装Homebrew 打开终端,输入: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ ...
- vue filter过滤器简单应用
vue中过滤器,用于一些常见的文本格式化,用 | 来操作. 过滤器可以用在两个地方: 1.在{{}}双花括号中插入值 2.v-bind表达式中使用 <!-- 在双花括号中 --> {{ m ...
- Kettle系列:Pentaho DI (Kettle) 下载地址
Kettle 8 已经发布, 下载地址还不太好找, 这里记录一下: 注: 所有大型软件升级都需要谨慎, 尤其是大版本的第一个小版本都不推荐在生产环境使用. github 总是有最新版 https:/ ...
- 二十一、Linux 进程与信号---进程资源限制
21.1 进程资源限制 在操作系统中,我们能够通过函数getrlimit().setrlimit()分别获得.设置每个进程能够创建的各种系统资源的限制使用量. 21.1.1 函数 #include & ...
- APPLE-SA-2019-3-25-7 Xcode 10.2
APPLE-SA-2019-3-25-7 Xcode 10.2 Xcode 10.2 is now available and addresses the following: KernelAvail ...
- matplotlib-区域填充
import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl import datetime #解决能显示中文 ...