python中自带的性能分析工具:
hotshot、profile、cProfile、line_profiler
hotshot:在python2中做性能分析还行,但是在python3中就不行了
profile:原始的纯python分析器
cProfile:具有profile相同的接口,是默认的分析工具
line_profiler:一行一行的分析性能
工作中使用最多的cProfile和line_profile
cProfile
import cProfile
import re
cProfile.run('re.compile("aaa")')
结果
99 function calls (98 primitive calls) in 0.000 seconds
#共有99个函数调用被监控,98个原生调用(不涉及递归)
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
#ncalls :函数被调用的次数。如果这一列有两个值,就表示有递归调用,第二个值是原生调用次数,第一个值是总调用次数
#tottime:函数内部消耗的总时间。
#percall :是tottime除以ncalls,一个函数每次调用平均消耗时间。
#cumtime :之前所有子函数消费时间的累计和
#percall :被分析函数所在文件名、行号、函数名。
# filename:lineno(function)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
2 0.000 0.000 0.000 0.000 enum.py:278(__call__)
2 0.000 0.000 0.000 0.000 enum.py:557(__new__)
1 0.000 0.000 0.000 0.000 enum.py:828(__and__)
1 0.000 0.000 0.000 0.000 re.py:250(compile)
1 0.000 0.000 0.000 0.000 re.py:289(_compile)
1 0.000 0.000 0.000 0.000 sre_compile.py:432(_generate_overlap_table)
1 0.000 0.000 0.000 0.000 sre_compile.py:453(_get_iscased)
1 0.000 0.000 0.000 0.000 sre_compile.py:461(_get_literal_prefix)
1 0.000 0.000 0.000 0.000 sre_compile.py:536(_compile_info)
2 0.000 0.000 0.000 0.000 sre_compile.py:595(isstring)
1 0.000 0.000 0.000 0.000 sre_compile.py:598(_code)
1 0.000 0.000 0.000 0.000 sre_compile.py:71(_compile)
1 0.000 0.000 0.000 0.000 sre_compile.py:759(compile)
1 0.000 0.000 0.000 0.000 sre_parse.py:111(__init__)
1 0.000 0.000 0.000 0.000 sre_parse.py:160(__len__)
3 0.000 0.000 0.000 0.000 sre_parse.py:164(__getitem__)
3 0.000 0.000 0.000 0.000 sre_parse.py:172(append)
1 0.000 0.000 0.000 0.000 sre_parse.py:174(getwidth)
1 0.000 0.000 0.000 0.000 sre_parse.py:224(__init__)
4 0.000 0.000 0.000 0.000 sre_parse.py:233(__next)
1 0.000 0.000 0.000 0.000 sre_parse.py:249(match)
3 0.000 0.000 0.000 0.000 sre_parse.py:254(get)
1 0.000 0.000 0.000 0.000 sre_parse.py:286(tell)
1 0.000 0.000 0.000 0.000 sre_parse.py:435(_parse_sub)
1 0.000 0.000 0.000 0.000 sre_parse.py:493(_parse)
1 0.000 0.000 0.000 0.000 sre_parse.py:76(__init__)
2 0.000 0.000 0.000 0.000 sre_parse.py:81(groups)
1 0.000 0.000 0.000 0.000 sre_parse.py:921(fix_flags)
1 0.000 0.000 0.000 0.000 sre_parse.py:937(parse)
1 0.000 0.000 0.000 0.000 {built-in method _sre.compile}
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
10 0.000 0.000 0.000 0.000 {built-in method builtins.isinstance}
13/12 0.000 0.000 0.000 0.000 {built-in method builtins.len}
3 0.000 0.000 0.000 0.000 {built-in method builtins.min}
3 0.000 0.000 0.000 0.000 {built-in method builtins.ord}
21 0.000 0.000 0.000 0.000 {method 'append' of 'list' objects}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
2 0.000 0.000 0.000 0.000 {method 'extend' of 'list' objects}
1 0.000 0.000 0.000 0.000 {method 'items' of 'dict' objects}
Process finished with exit code 0