SRo*_*mes 6 python profiler profiling
Python 包含一个简单易用的分析器:
>> import cProfile
>> import re
>> cProfile.run('re.compile("foo|bar")')
197 function calls (192 primitive calls) in 0.002 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.001 0.001 <string>:1(<module>)
1 0.000 0.000 0.001 0.001 re.py:212(compile)
...
Run Code Online (Sandbox Code Playgroud)
如何做完全相同的事情,但是按而tottime
不是排序standardname
?
使用sort=...
的参数cProfile.run
:
>>> import cProfile
>>> import time
>>> cProfile.run('time.sleep(1); time.monotonic()', sort='tottime')
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 1.001 1.001 1.001 1.001 {built-in method time.sleep}
1 0.000 0.000 1.001 1.001 {built-in method builtins.exec}
1 0.000 0.000 1.001 1.001 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {built-in method time.monotonic}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
Run Code Online (Sandbox Code Playgroud)