前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python模块:profile,pst

python模块:profile,pst

作者头像
py3study
发布2020-01-08 11:30:18
3950
发布2020-01-08 11:30:18
举报
文章被收录于专栏:python3python3

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档