python模塊:profile,pst

     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]#