Python - 按时间排序个人资料报告

问题描述 投票:0回答:2

Python 包含一个简单易用的 profiler

>> 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)
    ...

如何做完全相同的事情,但按

tottime
而不是
standardname
排序?

python profiling profiler
2个回答
2
投票

使用

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}

0
投票

您还可以在从命令行运行

tottime
时按
cProfile
排序,如下所示:

python -m cProfile -s tottime my_script.py
© www.soinside.com 2019 - 2024. All rights reserved.