哪些模块可用于python性能分析_python性能分析之cProfile模块

cProfile是标准库内建的分析工具的其中一个,另外两个是hotshot和profile

#-s cumulative -s cumulative开关告诉cProfile对每个函数累计花费的时间进行排序,他能让我看到代码最慢的部分。 我们有这样一个函数。 loopdemo.py

def foo():

for a in range(0, 101):

for b in range(0, 101):

if a + b == 100:

yield a, b

if __name__ == '__main__':

for item in foo():

print(item)

运行下面命令

python3 -m cProfile -s cumulative loopdemo.py

得到如下结果

206 function calls in 0.001 seconds

#在0.01秒内共发生了206次函数调用。包括cProfile的开销。

Ordered by: cumulative time

ncalls tottime percall cumtime percall filename:lineno(function)

1 0.000 0.000 0.001 0.001 {built-in method builtins.exec}

1 0.000 0.000 0.001 0.001 loopdemo.py:7()

102 0.001 0.000 0.001 0.000 loopdemo.py:7(foo)

101 0.001 0.000 0.001 0.000 {built-in method builtins.print}

1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}

其中对参数的解释: ncalls:表示函数调用的次数; tottime:表示指定函数的总的运行时间,除掉函数中调用子函数的运行时间; percall:(第一个percall)等于 tottime/ncalls; cumtime:表示该函数及其所有子函数的调用运行的时间,即函数开始调用到返回的时间; percall:(第二个percall)即函数运行一次的平均时间,等于 cumtime/ncalls; filename:lineno(function):每个函数调用的具体信息; 需要注意的是cProfile很难搞清楚函数内的每一行发生了什么,是针对整个函数来说的。 ###-o profile.stats 我们可与你通过这个函数将结果输出到一个文件中,当然文件的后缀名是任意的,这里为了方便后面配合python中使用所以将后缀定为stats。 首先让我们运行下面的命令

python3 -m cProfile -o loopdemo_profile.stats loopdemo.py

然后运行下面的脚本

import pstats

p=pstats.Stats("loopdemo_profile.stats")

p.sort_stats("cumulative")

p.print_stats()

p.print_callers() # 可以显示函数被哪些函数调用

p.print_callees() # 可以显示哪个函数调用了哪些函数

可以看到输出了和之前控制台一样的结果

2006 function calls in 0.005 seconds

Ordered by: cumulative time

ncalls tottime percall cumtime percall filename:lineno(function)

1 0.000 0.000 0.005 0.005 {built-in method builtins.exec}

1 0.000 0.000 0.005 0.005 loopdemo.py:7()

1001 0.004 0.000 0.004 0.000 {built-in method builtins.print}

1002 0.000 0.000 0.000 0.000 loopdemo.py:30(foo2)

1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}

Ordered by: cumulative time

Function was called by...

ncalls tottime cumtime

{built-in method builtins.exec}

loopdemo.py:7()

{built-in method builtins.print} )

loopdemo.py:30(foo2) )

{method 'disable' of '_lsprof.Profiler' objects}

Ordered by: cumulative time

Function called...

ncalls tottime cumtime

{built-in method builtins.exec} -> 1 0.000 0.005 loopdemo.py:7()

loopdemo.py:7() -> 1002 0.000 0.000 loopdemo.py:30(foo2)

1001 0.004 0.004 {built-in method builtins.print}

{built-in method builtins.print} ->

loopdemo.py:30(foo2) ->

{method 'disable' of '_lsprof.Profiler' objects} ->

###line_profiler 安装

pip3 install Cpython

pip3 install Cython git+https://github.com/rkern/line_profiler.git

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值