[转]Python Profile 工具性能分析



原文地址:http://chenxiaoyu.org/2013/08/28/python-profile.html


Python Profile 工具性能分析

最近碰到“程序速度大大降低”的说法,还是直接用数据说明比较有信服力,以及可以找出真正问题所在。

Python自带了几个性能分析的模块:profile、cProfile和hotshot,使用方法基本都差不多,无非模块是纯Python还是用C写的。

官网文档参考:http://docs.python.org/2/library/profile.html

本文示例基于cProfile模块,先写点测试代码(test1.py):

import time

def func1():
    sum = 0
    for i in range(1000000):
        sum += i

def func2():
    time.sleep(10)

func1()
func2()

运行cProfile命令如下:

$ python -m cProfile -o test1.out test1.py

这里是以模块方式直接保存profile结果,当然也可以在程序中引入cProfile模块。

好了,上面的测试程序大概运行10秒左右,可以看下最终的性能分析数据。

$ python -c "import pstats; p=pstats.Stats('test1.out'); p.print_stats()"

Wed Aug 28 22:19:45 2013    test1.out

         6 function calls in 10.163 seconds

   Random listing order was used

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.024    0.024   10.163   10.163 test1.py:1(<module>)
        1   10.001   10.001   10.001   10.001 {time.sleep}
        1    0.092    0.092    0.138    0.138 test1.py:3(func1)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.000    0.000   10.001   10.001 test1.py:8(func2)
        1    0.046    0.046    0.046    0.046 {range}

分析的数据还很清晰和简单的吧,大概能看出几列数据的意思,重点关注下时间和函数调用次数,再来排个序:

$ python -c "import pstats; p=pstats.Stats('test1.out'); p.sort_stats('time').print_stats()"

Wed Aug 28 22:19:45 2013    test1.out

         6 function calls in 10.163 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1   10.001   10.001   10.001   10.001 {time.sleep}
        1    0.092    0.092    0.138    0.138 test1.py:3(func1)
        1    0.046    0.046    0.046    0.046 {range}
        1    0.024    0.024   10.163   10.163 test1.py:1(<module>)
        1    0.000    0.000   10.001   10.001 test1.py:8(func2)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

可以看出测试程序中最耗费时间点是:time.sleep和range。

sort_stats支持一下参数:

calls, cumulative, file, line, module, name, nfl, pcalls, stdname, time

pstats模块还支持交互式:

$ python -m pstats test1.out
Welcome to the profile statistics browser.
test1.out% ### help 或者输入字母按下tab,可以补全的
test1.out% stats 10

这里似乎告一段落了,只是输出式的性能分析似乎有点乏味,开源世界这么美好,肯定会有些更美观和帅气的方法吧。

一步步来,先安装依赖项(请选择对应系统平台):

PS:Ubuntu系统请注意请注意,pstats模块因为某些原因不在Python包中,需要单独安装:python-profiler包

装好依赖后,如此这番:

$ ./gprof2dot.py -f pstats test1.out | dot -Tpng -o test1.png

看看test1.png,一切都了然于心了吧。。。

python profile

END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值