前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python性能分析之cProfile模块

python性能分析之cProfile模块

作者头像
狼啸风云
修改2022-09-03 19:26:31
3.6K0
修改2022-09-03 19:26:31
举报

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

-s cumulative

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

代码语言:javascript
复制
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)

运行下面命令

代码语言:javascript
复制
python3 -m cProfile -s cumulative loopdemo.py

得到如下结果

代码语言:javascript
复制
         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(<module>)
      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。 首先让我们运行下面的命令

代码语言:javascript
复制
python3 -m cProfile -o loopdemo_profile.stats loopdemo.py

然后运行下面的脚本

代码语言:javascript
复制
import pstats
p=pstats.Stats("loopdemo_profile.stats")
p.sort_stats("cumulative")
p.print_stats()
p.print_callers()  # 可以显示函数被哪些函数调用
p.print_callees()  # 可以显示哪个函数调用了哪些函数

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

代码语言:javascript
复制
         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(<module>)
     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(<module>)                           <-       1    0.000    0.005  {built-in method builtins.exec}
{built-in method builtins.print}                  <-    1001    0.004    0.004  loopdemo.py:7(<module>)
loopdemo.py:30(foo2)                              <-    1002    0.000    0.000  loopdemo.py:7(<module>)
{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(<module>)
loopdemo.py:7(<module>)                           ->    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

安装

代码语言:javascript
复制
pip3 install Cpython
pip3 install Cython git+https://github.com/rkern/line_profiler.git
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • -s cumulative
    • -o profile.stats
      • line_profiler
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档