用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经授权转载,版权归原作者所有. 微信小程序如果想要优化性能,有关键 ...
随机推荐
- Element-UI 表格 列过多内容换行问题
本文地址:http://www.cnblogs.com/veinyin/p/8487098.html 一般表格不会有很多列,所以在使用时会很方便,但是如果有25+个列时,就会发现宽度完全不够用,只有 ...
- input新类型详解
http://www.webhek.com/post/html5-input-type.html
- 20165320 预备作业3 :Linux安装及命令入门
一.VirtualBox与Linux的安装 我是按照老师给的链接下的最新版本的VirtualBox5.26,然后Ubuntu软件(版本是16.04,最新的是17)是自己在网上找的旧版本下好的,因为我在 ...
- js原生选择class DOM元素
document.querySelector(".demo"); querySelector() 方法返回匹配指定选择器的第一个元素.如果需要返回所有的元素,使用 querySel ...
- 正则表达式&自定义异常 典型案例
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static vo ...
- Linux内核Ramdisk(initrd)机制【转】
转自:http://www.cnblogs.com/armlinux/archive/2011/03/30/2396827.html 摘要:对于Linux用户来说,Ramdisk并不陌生,可是为什么需 ...
- php CI框架
CodeIgniter 是一个小巧但功能强大的 PHP 框架,作为一个简单而“优雅”的工具包,它可以为 PHP 程序员建立功能完善的 Web 应用程序.如果你是一个使用共享主机,并且为客户所要求的期限 ...
- ASP .Net Core系统部署到SUSE 16 Linux Enterprise Server 12 SP2 64 具体方案
.Net Core 部署到 SUSE 16 Linux Enterprise Server 12 SP2 64 位中的步骤 1.安装工具 1.apache 2..Net Core(dotnet-sdk ...
- echarts一些笔记
console.log(); 浏览器显示 $.ajax({ url : "ajax/echartWelcome.action", type : "post", ...
- thinkphp辅助方法,数据库操作