[Python]程序性能分析
有些脚本发现比预期要慢的多,就需要找到瓶颈,然后做相应的优化,参考A guide to analyzing Python performance,也可以说是翻译。
指标
- 运行时间
- 时间瓶颈
- 内存使用
- 是否有内存泄漏
基本
linux time
这是个shell中自带的命令,也是最简单和方面的方法,但是得到信息太少
[root@bogon util]# time python pvsts.py
Yesterday PV/UV
PV 46300
UV is 3899
real 2m36.591s #花费时间
user 2m37.167s #用户态时间
sys 0m2.010s #内核态时间
如果 sys
+user
比 real
小的多,就要考虑io等待时间是否过长了。
使用Cprofile工具
用起来很简单,显示的东西也很多,但是对于代码
来说不是很直观
[root@bogon util]# python -m cProfile pvsts.py
Yesterday PV/UV
PV 46300
UV is 3899
502249600 function calls (502249597 primitive calls) in 250.221 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 250.221 250.221 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 __future__.py:48(<module>)
1 0.000 0.000 0.000 0.000 __future__.py:74(_Feature)
7 0.000 0.000 0.000 0.000 __future__.py:75(__init__)
1 0.000 0.000 0.000 0.000 __init__.py:49(normalize_encoding)
1 0.000 0.000 0.000 0.000 __init__.py:71(search_function)
1 0.000 0.000 0.000 0.000 base64.py:3(<module>)
测试时间工具line_profiler
就是这个小工具,安装很simple
$ pip install line_profiler
在想要测试的函数上添加一个 @profile
装饰器(不用倒入任何包,工具会自动倒入)
@profile
def sts_uv():
#mac_list = []
mac_set = set()
with open(temp_log, 'r') as f:
for line in f.readlines():
basid, mac, ip = decode_token(str(line.strip()))
#mac_list.append(mac)
mac_set.add(mac)
#uv = len(set(mac_list))
uv = len(mac_set)
print "UV is {0}".format(uv)
return uv
得到结果:
[root@bogon util]# kernprof -l -v pvsts.py
Yesterday PV/UV
PV 46300
UV is 3899
Wrote profile results to pvsts.py.lprof
Timer unit: 1e-06 s
Total time: 450.299 s
File: pvsts.py
Function: sts_uv at line 74
Line # Hits Time Per Hit % Time Line Contents
==============================================================
74 @profile
75 def sts_uv():
76 #mac_list = []
77 1 10 10.0 0.0 mac_set = set()
78 1 59 59.0 0.0 with open(temp_log, 'r') as f:
79 42431 38556 0.9 0.0 for line in f.readlines():
80 42430 450188794 10610.2 100.0 basid, mac, ip = decode_token(str(line.strip()))
81 #mac_list.append(mac)
82 42430 71491 1.7 0.0 mac_set.add(mac)
83 #uv = len(set(mac_list))
84 1 2 2.0 0.0 uv = len(mac_set)
85 1 15 15.0 0.0 print "UV is {0}".format(uv)
86 1 1 1.0 0.0 return uv
同时还是会生成一个pvsts.py.lprof
文件
测试内存使用 pip install -U memory_profiler
安装两个工具
$ pip install -U memory_profiler
$ pip install psutil
使用上也是添加一个 ‘@profile’ 装饰器,跟上面的一样。
测试
[root@bogon util]# python -m memory_profiler pvsts.py
Yesterday PV/UV
PV 46300
UV is 3899
Filename: pvsts.py
Line # Mem usage Increment Line Contents
================================================
74 9.676 MiB 0.000 MiB @profile
75 def sts_uv():
76 #mac_list = []
77 9.676 MiB 0.000 MiB mac_set = set()
78 9.676 MiB 0.000 MiB with open(temp_log, 'r') as f:
79 15.289 MiB 5.613 MiB for line in f.readlines():
80 15.289 MiB 0.000 MiB basid, mac, ip = decode_token(str(line.strip()))
81 #mac_list.append(mac)
82 15.289 MiB 0.000 MiB mac_set.add(mac)
83 #uv = len(set(mac_list))
84 14.961 MiB -0.328 MiB uv = len(mac_set)
85 14.961 MiB 0.000 MiB print "UV is {0}".format(uv)
86 14.961 MiB 0.000 MiB return uv
声明:
本文出自 “orangleliu笔记本” 博客,转载请务必保留此出处http://blog.csdn.net/orangleliu/article/details/45934005 作者orangleliu 采用署名-非商业性使用-相同方式共享协议
[Python]程序性能分析的更多相关文章
- Python程序性能分析模块----------cProfile
cProfile分析器可以用来计算程序整个运行时间,还可以单独计算每个函数运行时间,并且告诉你这个函数被调用多少次 def foo(): pass import cProfile cProfile.r ...
- python程序性能分析
中文:http://www.cnblogs.com/zhouej/archive/2012/03/25/2379646.html 英文:https://www.huyng.com/posts/pyth ...
- Linux下的应用程序性能分析 总结
Linux下的应用程序性能分析,根据内核程序和应用程序的不同,下文分两类进行描述. 我们侧重的是应用级别的程序,推荐google perf tool/kcachegrind组合 一.和内核有关的工具 ...
- Linux程序性能分析和火焰图
Linux程序性能分析和火焰图 Linux程序的性能分析工具数量比较多,涉及到整个操作系统的方方面面,可能是开源的原因吧,相对于Windows来说丰富太多.其中应用分析性能方面Dtrace, Syst ...
- 八、jdk工具之JvisualVM、JvisualVM之二--Java程序性能分析工具Java VisualVM
目录 一.jdk工具之jps(JVM Process Status Tools)命令使用 二.jdk命令之javah命令(C Header and Stub File Generator) 三.jdk ...
- Golang程序性能分析
前言 程序性能分析我相信是每个程序员都会遇到的问题,比如说一个程序的CPU为什么占用这么高?有没有优化的空间?又比如程序出现了内存泄漏如何排查等等.如果是C++程序会借助于Google pprof c ...
- 一个python 服务器程序性能分析
该服务器为bono,启动11个进程. 1.设置cprofile 在启动服务的总入口设置cprofile if __name__=="__main__": import cProfi ...
- 转帖:Python应用性能分析指南
原文:A guide to analyzing Python performance While it’s not always the case that every Python program ...
- [golang]7种 Go 程序性能分析方法
视频信息 Seven ways to Profile Go Applicationsby Dave Cheneyat Golang UK Conf. 2016 视频:https://www.youtu ...
随机推荐
- 【SPOJ839】Optimal Marks 网络流
You are given an undirected graph G(V, E). Each vertex has a mark which is an integer from the range ...
- ●BZOJ 1767 [Ceoi2009]harbingers
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1767 题解: 斜率优化DP,单调栈,二分 定义 DP[i] 表示从 i 节点出发,到达根所花 ...
- 基于Mapxtreme for JAVA的电子地图设计与实现
基于Mapxtreme for JAVA的电子地图设计与实现学生毕业设计,适合测绘类专业研究目标: 开发一个基于MapXtreme for JAVA的校园电子地图项目,使用MapInfo ...
- Python中模块之os的功能介绍
Python中模块之os的功能介绍 1. os的变量 path 模块路径 方法:os.path 返回值:module 例如:print(os.path) >>> <module ...
- Miox带你走进动态路由的世界——51信用卡前端团队
写在前面: 有的时候再做大型项目的时候,确实会被复杂的路由逻辑所烦恼,会经常遇到权限问题,路由跳转回退逻辑问题.这几天在网上看到了51信用卡团队开源了一个Miox,可以有效的解决这些痛点,于是乎我就做 ...
- 用background-image做成条纹背景
效果: 实现: //html <div class="container"> <span class="tip span-1">1111 ...
- Uncaught (in promise) TypeError:的错误
1.错误 创建一个vue实例,在data定义一些变量,如activityTime. 在methods里面用了axios发送请求. 在then的回调里使用this.activityTime 报错! 2. ...
- Java线程池使用和常用参数
多线程问题: 1.java中为什么要使用多线程使用多线程,可以把一些大任务分解成多个小任务来执行,多个小任务之间互不影像,同时进行,这样,充分利用了cpu资源. 2.java中简单的实现多线程的方式 ...
- gravity和layout_gravity的区别
一.gravity和layout_gravity相同处 两者都是设置对齐方式的属性.内部的属性值相同. 根据英文意思也能理解其中的意思.如center_horizontal表示在水平方向上的位置为中间 ...
- Vulkan的分层设计
Vulkan驱动层提供了简单高效的API.作为Vulkan API的使用者,我们要严格遵循Vulkan API的使用规则.如果我们违反了这些规则,Vulkan只会返回很少的反馈,它只会报告一部分严重和 ...