用profile协助程序性能优化
|
def foo():
sum = 0
for i in range(100):
sum += i
return sum
if __name__ == "__main__":
foo()
|
|
if __name__ == "__main__":
import profile
profile.run("foo()")
|
|
5 function calls in 0.143 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 :0(range)
1 0.143 0.143 0.143 0.143 :0(setprofile)
1 0.000 0.000 0.000 0.000 <string>:1(?)
1 0.000 0.000 0.000 0.000 prof1.py:1(foo)
1 0.000 0.000 0.143 0.143 profile:0(foo())
0 0.000 0.000 profile:0(profiler)
|
python -m profile prof1.py |
|
ncalls
|
函数的被调用次数
|
|
tottime
|
函数总计运行时间,除去函数中调用的函数运行时间
|
|
percall
|
函数运行一次的平均时间,等于tottime/ncalls
|
|
cumtime
|
函数总计运行时间,含调用的函数运行时间
|
|
percall
|
函数运行一次的平均时间,等于cumtime/ncalls
|
|
filename:lineno(function)
|
函数所在的文件名,函数的行号,函数名
|
|
# …略
if __name__ == "__main__":
import profile
profile.run("foo()", "prof.txt")
import pstats
p = pstats.Stats("prof.txt")
p.sort_stats("time").print_stats()
|
|
Sun Jan 14 00:03:12 2007 prof.txt
5 function calls in 0.002 CPU seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.002 0.002 0.002 0.002 :0(setprofile)
1 0.000 0.000 0.002 0.002 profile:0(foo())
1 0.000 0.000 0.000 0.000 G:/prof1.py:1(foo)
1 0.000 0.000 0.000 0.000 <string>:1(?)
1 0.000 0.000 0.000 0.000 :0(range)
0 0.000 0.000 profile:0(profiler)
|
|
strip_dirs()
|
用以除去文件名前名的路径信息。
|
|
add(filename,[…])
|
把profile的输出文件加入Stats实例中统计
|
|
dump_stats(filename)
|
把Stats的统计结果保存到文件
|
|
sort_stats(key,[…])
|
最重要的一个函数,用以排序profile的输出
|
|
reverse_order()
|
把Stats实例里的数据反序重排
|
|
print_stats([restriction,…])
|
把Stats报表输出到stdout
|
|
print_callers([restriction,…])
|
输出调用了指定的函数的函数的相关信息
|
|
print_callees([restriction,…])
|
输出指定的函数调用过的函数的相关信息
|
|
‘ncalls’
|
被调用次数
|
|
‘cumulative’
|
函数运行的总时间
|
|
‘file’
|
文件名
|
|
‘module’
|
文件名
|
|
‘pcalls’
|
简单调用统计(兼容旧版,未统计递归调用)
|
|
‘line’
|
行号
|
|
‘name’
|
函数名
|
|
‘nfl’
|
Name/file/line
|
|
‘stdname’
|
标准函数名
|
|
‘time’
|
函数内部运行时间(不计调用子函数的时间)
|
p.strip_dirs().sort_stats(-1).print_stats()
|
# …略
if __name__ == "__main__":
import hotshot
import hotshot.stats
prof = hotshot.Profile("hs_prof.txt", 1)
prof.runcall(foo)
prof.close()
p = hotshot.stats.load("hs_prof.txt")
p.print_stats()
|
|
1 function calls in 0.003 CPU seconds
Random listing order was used
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.003 0.003 0.003 0.003 i:/prof1.py:1(foo)
0 0.000 0.000 profile:0(profiler)
|
|
run(cmd)
|
执行一段脚本,跟profile模块的run()函数一样功能
|
|
runcall(func, *args, **keywords)
|
调用一个函数,并统计相关的运行信息
|
|
runctx(cmd, globals, locals)
|
指定一段脚本的执行环境,执行脚本并统计运行信息
|
|
>>> t = timeit.Timer("t = foo()/nprint t") ß被timeit的代码段
>>> t.timeit()
Traceback (most recent call last):
File "<pyshell#12>", line 1, in -toplevel-
t.timeit()
File "E:/Python23/lib/timeit.py", line 158, in timeit
return self.inner(it, self.timer)
File "<timeit-src>", line 6, in inner
foo() ß标准输出是这样的
NameError: global name 'foo' is not defined
>>> try:
t.timeit()
except:
t.print_exc()
Traceback (most recent call last):
File "<pyshell#17>", line 2, in ?
File "E:/Python23/lib/timeit.py", line 158, in timeit
return self.inner(it, self.timer)
File "<timeit-src>", line 6, in inner
t = foo() ßprint_exc()的输出是这样的,方便定位错误
NameError: global name 'foo' is not defined
|
用profile协助程序性能优化的更多相关文章
- [python]用profile协助程序性能优化
转自:http://blog.csdn.net/gzlaiyonghao/article/details/1483728 本文最初发表于恋花蝶的博客http://blog.csdn.net/lanph ...
- [深入浅出Cocoa]iOS程序性能优化
本文转载至 http://blog.csdn.net/kesalin/article/details/8762032 [深入浅出Cocoa]iOS程序性能优化 罗朝辉 (http://blog.csd ...
- C++ 应用程序性能优化
C++ 应用程序性能优化 eryar@163.com 1. Introduction 对于几何造型内核OpenCASCADE,由于会涉及到大量的数值算法,如矩阵相关计算,微积分,Newton迭代法解方 ...
- Java程序性能优化技巧
Java程序性能优化技巧 多线程.集合.网络编程.内存优化.缓冲..spring.设计模式.软件工程.编程思想 1.生成对象时,合理分配空间和大小new ArrayList(100); 2.优化for ...
- 《Java程序性能优化:让你的Java程序更快、更稳定》
Java程序性能优化:让你的Java程序更快.更稳定, 卓越网更便宜,不错的书吧
- [JAVA] java程序性能优化
一.避免在循环条件中使用复杂表达式 在不做编译优化的情况下,在循环中,循环条件会被反复计算,如果不使用复杂表达式,而使循环条件值不变的话,程序将会运行的更快. 例子: import java.util ...
- iOS程序性能优化
iOS程序性能优化 一.初级 使用ARC进行内存管理 在iOS5发布的ARC,它解决了最常见的内存泄露问题.但是值得注意的是,ARC并不能避免所有的内存泄露.使用ARC之后,工程中可能还会有内存泄露, ...
- iOS 程序性能优化
前言 转载自:http://www.samirchen.com/ios-performance-optimization/ 程序性能优化不应该是一件放在功能完成之后的事,对性能的概念应该从我们一开始写 ...
- 微信小程序性能优化技巧
摘要: 如果小程序不够快,还要它干嘛? 原文:微信小程序性能优化方案--让你的小程序如此丝滑 作者:杜俊成要好好学习 Fundebug经授权转载,版权归原作者所有. 微信小程序如果想要优化性能,有关键 ...
随机推荐
- Trying to get property of non-object
原文:http://www.jb51.net/article/29878.htm 总结:判断为空用 if(isset($v)){}代替if($v == null){}
- MySQL练习-主外键多表查询
练习: 1.建立表关系: 请创建如下表,并创建相关约束 USE db1; CREATE TABLE class( cid INT AUTO_INCREMENT PRIMARY KEY, caption ...
- C. Connect Three(构造)
题目链接:http://codeforces.com/contest/1087/problem/C 题目大意:给你三个点的坐标,让你用尽可能少的方块,让这三个点连起来. 具体思路: 我们先对这三个点进 ...
- hdu 确定比赛名次(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) ...
- linux下定时器介绍2--timer_create等函数集的使用示例
程序1:采用新线程派驻的通知方式 程序2:通知方式为信号的处理方式 #include <stdio.h>#include <time.h>#include <stdlib ...
- Jenkins+Ant+TestNG+Testlink自动化构建集成
这段时间折腾自动化测试,之前都是在Eclipse工程里面手工执行自动化测试脚本,调用Testlink API执行测试用例,目前搭建Jenkins自动化构建测试的方式,实现持续构建,执行自动化测试. 硬 ...
- spring学习之一概念
概念 1.是开源的轻量级框架 2.是一站式框架,就是说在java ee的三层结构中,每一层它都提供了不同的解决技术 web层:springMVC servoce层:spring IOC ,控制反转,通 ...
- docker 部署 portainer(http)
=============================================== 2019/4/30_第6次修改 ccb_warlock 更新 ...
- 使用Scrapy命令行工具【导出JSON文件】时编码设置
Windows 10家庭中文版,Python 3.6.4,virtualenv 16.0.0,Scrapy 1.5.0, 使用scrapy命令行工具建立了爬虫项目(startproject),并使用s ...
- ASP.NET Web Api OwinSelfHost Restful 使用
一.前言 总结一下什么是RESTful架构: (1)每一个URI代表一种资源: (2)客户端和服务器之间,传递这种资源的某种表现层: (3)客户端通过四个HTTP动词,对服务器端资源进行操作,实现&q ...