用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经授权转载,版权归原作者所有. 微信小程序如果想要优化性能,有关键 ...
随机推荐
- 基本控件文档-UISegment属性
CHENYILONG Blog 基本控件文档-UISegment属性 Fullscreen UISegment属性技术博客http://www.cnblogs.com/ChenYilong/ 新浪 ...
- 【leetcode 简单】 第八十六题 有效的完全平方数
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False. 注意:不要使用任何内置的库函数,如 sqrt. 示例 1: 输入: 16 输出: Tr ...
- 2017 ACM暑期多校联合训练 - Team 9 1008 HDU 6168 Numbers (模拟)
题目链接 Problem Description zk has n numbers a1,a2,...,an. For each (i,j) satisfying 1≤i<j≤n, zk gen ...
- spring-boot-单元测试参数数
简单案例 @RunWith(Parameterized.class) public class ParameterTest { // 2.声明变量存放预期值和测试数据 private String f ...
- Java学习笔记——继承、接口、多态
浮点数的运算需要注意的问题: BigDecimal operand1 = new BigDecimal("1.0"); BigDecimal operand2 = new BigD ...
- SpringMVC控制器 跳转到jsp页面 css img js等文件不起作用 不显示
今天在SpringMVC转发页面的时候发现跳转页面确实成功,但是JS,CSS等静态资源不起作用: 控制层代码: /** * 转发到查看培养方案详情的页面 * @return */ @RequestMa ...
- 大规模实时流处理平台架构-zz
随着不同网络质量下接入终端设备种类的增多,服务端转码已经成为视频点播和直播产品中必备的能力之一.直播产品讲究时效性,希望在一定的时间内让所有终端看到不同尺寸甚至是不同质量的视频,因此对转码的实时性要求 ...
- linux下定时器介绍1
POSIX Timer 间隔定时器 setitimer 有一些重要的缺点,POSIX Timer 对 setitimer 进行了增强,克服了 setitimer 的诸多问题: 首先,一个进程同一时刻只 ...
- CF529B 【Group Photo 2 (online mirror version)】
贪心枚举最后方案中最大的h,设为maxh若某个人i的wi与hi均大于maxh,则此方案不可行若某个人恰有一个属性大于maxh,则可确定他是否换属性剩下的人按wi-hi从大到小排序后贪心选择O(nlog ...
- sqlserver中的全局变量总结
@@CONNECTIONS返回自上次启动 Microsoft? SQL Server? 以来连接或试图连接的次数.@@CPU_BUSY返回自上次启动 Microsoft? SQL Server? 以来 ...