profile和pstats是python代码的分析器,可以很客观查看代码的运行质量和使用的资源.在调试程序时有很大的帮助.
1.使用profile分析python的代码
[root@node1 tmp]# vim profile12.py
#!/bin/env python
#!-*- coding:UTF-8 -*-
import profile
def one(): #定义一个one函数
sum=0
for i in range(10000):
sum+=i
return sum
def two():
sum=0
for i in range(100000):
sum+=i
return sum
def there():
sum=0
for i in range(100000):
sum+=i
return sum
if __name__=="__main__":
profile.run("one()","result") #将结果保存到result文件中
profile.run("two()")
profile.run("there()")
[root@node1 tmp]# python profile12.py
5 function calls in 0.010 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.003 0.003 0.003 0.003 :0(range)
1 0.000 0.000 0.000 0.000 :0(setprofile)
1 0.000 0.000 0.010 0.010 <string>:1(<module>)
1 0.007 0.007 0.010 0.010 profile12.py:12(two)
0 0.000 0.000 profile:0(profiler)
1 0.000 0.000 0.010 0.010 profile:0(two())
ncalls:函数调用的次数
tottime:函数的总的运行时间,除掉函数中调用子函数的运行时间
percall:(第一个 percall)等于tottime/ncalls
cumtime:函数及其所有子函数的调用运行的时间,即函数开始调用到返回的时间
percall:(第二个 percall)即函数运行一次的平均时间,等于 cumtime/ncalls
filename:lineno(function):每个函数调用的具体信息
5 function calls in 0.008 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.001 0.001 0.001 0.001 :0(range)
1 0.000 0.000 0.000 0.000 :0(setprofile)
1 0.000 0.000 0.008 0.008 <string>:1(<module>)
1 0.007 0.007 0.008 0.008 profile12.py:18(there)
0 0.000 0.000 profile:0(profiler)
1 0.000 0.000 0.008 0.008 profile:0(there())
Thu May 5 17:30:09 2016 result
5 function calls in 0.001 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.000 0.000 0.000 0.000 :0(setprofile)
1 0.000 0.000 0.001 0.001 <string>:1(<module>)
1 0.001 0.001 0.001 0.001 profile12.py:6(one)
1 0.000 0.000 0.001 0.001 profile:0(one())
0 0.000 0.000 profile:0(profiler)
[root@node1 tmp]#
2.使用pstats分析python代码
[root@node1 tmp]# vim profile12.py
#!/bin/env python
#!-*- coding:UTF-8 -*-
import profile,pstats
def one():
sum=0
for i in range(10000):
sum+=i
return sum
if __name__=="__main__":
profile.run("one()","result") #将结果保存到result文件中
p=pstats.Stats("result") #创建一上pstats变量
p.strip_dirs().sort_stats(-1).print_stats() #strip_dirs:
从所有模块名中去掉无关的路径信息
p.strip_dirs().sort_stats("name").print_stats() #sort_stats():把打印信息按照标准的module/name/line字符串进行排序
p.strip_dirs().sort_stats("cumulative").print_stats(3) #print_stats():打印出所有分析信息
[root@node1 tmp]# python profile12.py
Thu May 5 17:54:49 2016 result
5 function calls in 0.001 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.000 0.000 0.000 0.000 :0(setprofile)
1 0.000 0.000 0.001 0.001 <string>:1(<module>)
1 0.001 0.001 0.001 0.001 profile12.py:6(one)
1 0.000 0.000 0.001 0.001 profile:0(one())
0 0.000 0.000 profile:0(profiler)
Thu May 5 17:54:49 2016 result
5 function calls in 0.001 CPU seconds
Ordered by: function name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.001 0.001 <string>:1(<module>)
1 0.001 0.001 0.001 0.001 profile12.py:6(one)
1 0.000 0.000 0.001 0.001 profile:0(one())
0 0.000 0.000 profile:0(profiler)
1 0.000 0.000 0.000 0.000 :0(range)
1 0.000 0.000 0.000 0.000 :0(setprofile)
Thu May 5 17:54:49 2016 result
5 function calls in 0.001 CPU seconds
Ordered by: cumulative time
List reduced from 6 to 3 due to restriction <3>
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.001 0.001 0.001 0.001 profile12.py:6(one)
1 0.000 0.000 0.001 0.001 profile:0(one())
1 0.000 0.000 0.001 0.001 <string>:1(<module>)
[root@node1 tmp]#
本文出自 “一起走过的日子” 博客,请务必保留此出处http://tongcheng.blog.51cto.com/6214144/1770508
原文地址:http://tongcheng.blog.51cto.com/6214144/1770508