用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经授权转载,版权归原作者所有. 微信小程序如果想要优化性能,有关键 ...
随机推荐
- 【转】C#中Graphics的画图代码
C#中Graphics的画图代码[转] 架上图片了你就可以在画板上涂改了啊 我要写多几个字上去string str = "Baidu"; //写什么字?Font font = Fo ...
- 38、使用IO流进行文件拷贝
使用IO流进行文件拷贝 需求:在项目的根目录里面创建一个java.txt的文件,然后将这个文件拷贝到file文件夹里面并且重命名为good.txt文件先以流的方式将java.txt文件读取到内存中,然 ...
- JQuery获取被选中的checkbox的value值
文章源头:http://www.cnblogs.com/td960505/p/6123510.html 以下为使用JQuery获取input checkbox被选中的值代码: <html> ...
- 容斥原理&&莫比乌斯专题
A题:A - Eddy's爱好 HDU - 2204 具体思路:如果是求n中,为平方数的有多少个,那么答案肯定是sqrt(n),同理,如果是三次根号的话,那么答案肯定是n的三分之一次方.然后继续按 ...
- 对web标准的理解,以及对w3c组织的认识
(1)web标准规范要求,书写标签必须闭合.标签小写.不乱嵌套,可提高搜索机器人对网页内容的搜索几率.--- SEO(2)建议使用外链css和js脚本,从而达到结构与行为.结构与表现的分离,提高页面的 ...
- Java中关于变量的几种情况
Java中关于变量的几种情况 1.继承时变量的引用关系 class Animals { int age = 10; void enjoy() { System.out.println("An ...
- Hibernate5笔记8--Hibernate事务相关内容
Hibernate事务相关内容: (1) 事务四大特性(简称ACID): (1)原子性(Atomicity) 事务中的全部操作在数据库中是不可分割的,要么全部完成,要么均不执行. (2)一致性(Con ...
- VMware 增加硬盘ubuntu
http://blog.csdn.net/Timsley/article/details/50742755
- C# 浅谈 接口(Interface)的作用
继承"基类"跟继承"接口"都能实现某些相同的功能,但有些接口能够完成的功能是只用基类无法实现的 1.接口用于描述一组类的公共方法/公共属性. 它不实现任何的方法 ...
- spring boot 中使用redis session
spring boot 默认的httpsession是存在内存中.这种默认方式有几个缺点:1.当分布式部署时,存在session不一致的问题:2.当服务重启时session就会丢失,这时候用户就需要重 ...