Ju Zi's blog

Back

用法#

API方式#

API方式允许我们以代码的方式来运行性能测试,API接口为:

import profile
profile.run(<function_name>, <file_name>, <sort>)
plaintext

首先需要导入profile模块,然后调用run函数。若未指定文件名,则会将分析结果打印出来。例:

命令行#

相比于API方式,使用命令行则会更加方便的分析我们已经写好的代码。语法:

不保存文件:python -m cProfile <code_file_name>.py

保存分析结果到文件:python -m cProfile -o <save_file_name> <code_file_name>.py

解析保存的文件#

在上一步中生成的文件并非文本文件,无法直接打开查看结果,需要先将文件解析出来。

解析需要用到pstats模块,一般直接在命令行中执行python脚本即可。

python -c "import pstats; p=pstats.Stats('<file_name>'); p.sort_stats('<sort>').print_stats()"

其中,sort_stats支持以下参数排序:

  • calls/ncalls:调用次数
  • cumtime/cumulative:累计时间
  • filename/module:文件名
  • line:行号
  • name:函数名
  • nfl:函数名,文件名,行号(name/file/line)
  • pcalls:原始调用次数
  • stdname:标准函数名
  • time/tottime:时间(不包括子函数时间)

参考:在pstats.py中找到如下定义

分析结果#

         9 function calls in 2.361 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       1    0.000    0.000    2.361    2.361 profile:0(test_code())
       1    0.000    0.000    2.361    2.361 :0(exec)
       1    0.000    0.000    2.361    2.361 <string>:1(<module>)
       1    0.000    0.000    2.361    2.361 <ipython-input-11-a4480b4512b3>:16(test_code)
       2    2.361    1.181    2.361    1.181 <ipython-input-11-a4480b4512b3>:4(cal_factorial)
       1    0.000    0.000    2.328    2.328 <ipython-input-11-a4480b4512b3>:13(functionB)
       1    0.000    0.000    0.033    0.033 <ipython-input-11-a4480b4512b3>:10(functionA)
       1    0.000    0.000    0.000    0.000 :0(setprofile)
       0    0.000             0.000          profile:0(profiler)
plaintext

各字段含义:

  • ncalls:调用次数
  • tottime:总时间(不包括子函数时间)
  • cumtime:累计时间(包括子函数时间)
  • percall:每次调用时间=总时间/调用次数
  • filename:文件名
  • lineno:行号
  • function:函数名

从分析结果中可以看出,本次运行test_code一共用了2.361秒,functionA用了0.033秒,functionB用了2.328秒,但他们函数本身都未消耗时间,时间都消耗在调用子函数:cal_factorial上。

Profile分析Python代码性能
https://juzzi.qzz.io/blog/lang/python/profile-ananyze-python-performance
Author Ju Zi
Published at March 8, 2021
Comment seems to stuck. Try to refresh?✨