python性能分析模块cprofile

February 27, 2024

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

我们有这样一个函数。

# 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(<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很难搞清楚函数内的每一行发生了什么,是针对整个函数来说的。

ncalls for the number of calls.

tottime for the total time spent in the given function (and excluding time made in calls to sub-functions)

percall is the quotient of tottime divided by ncalls

cumtime is the cumulative time spent in this and all subfunctions (from invocation till exit). This figure is accurate even for recursive functions.

percall is the quotient of cumtime divided by primitive calls

filename:lineno(function) provides the respective data of each function

-s cumulative -s cumulative开关告诉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(<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}  -> 

cProfile 和 profile 文件也可以作为脚本调用,以分析另一个脚本。例如:

python -m cProfile [-o output_file] [-s sort_order] (-m module | myscript.py)

-o 将profile 结果写入文件而不是标准输出

-s 指定 sort_stats() 排序值之一以对输出进行排序。这仅适用于未提供 -o 的情况

-m 指定要分析的是模块而不是脚本。

  1. The Python Profilers. (n.d.). Python 3.12.2 Documentation. Retrieved February 27, 2024, from https://docs.python.org/3/library/profile.html
  2. Wanderer001. (07 June, 2022.). python性能分析之cProfile模块. CSDN. [Blog post]. Retrieved February 27, 2024, from https://blog.csdn.net/weixin_36670529/article/details/103774677
  3. ElvinYang. (11 May, 2020.). Python 程序性能分析和火焰图. infoQ. [Blog post]. Retrieved February 28, 2024, from https://xie.infoq.cn/article/74483a0917668dc17324d0313

© 2024 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2024-02-27). python性能分析模块cprofile. https://caiborui.com/blog/2024-02-27-zh-python%E6%80%A7%E8%83%BD%E5%88%86%E6%9E%90%E6%A8%A1%E5%9D%97cprofile

@misc{borui2024-3729e2c6,
  author = {borui},
  title = {{python性能分析模块cprofile}},
  year = {2024},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2024-02-27-zh-python%E6%80%A7%E8%83%BD%E5%88%86%E6%9E%90%E6%A8%A1%E5%9D%97cprofile}
}