Python——cProfile(程序分析)

程序分析可以系统性地分析程序的运行速度、内存使用情况等。

cProfile是Python的分析器,用于测量程序的运行时间和程序内各个函数调用消耗的时间。

import  cProfile

def add():
    total = 0
    for i in range(1, 10000001):
        total += i

cProfile.run('add()')


'''
     4 function calls in 0.276 seconds

Ordered by: standard name

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.000    0.000    0.276    0.276 <string>:1(<module>)
    1    0.276    0.276    0.276    0.276 web_test.py:8(add)
    1    0.000    0.000    0.276    0.276 {built-in method builtins.exec}
    1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
'''

ncalls:对函数的调用次数

tottime:该函数花费的总时间,注意不包括在子函数中花费的时间

percall:tottime除以调用次数

cumtime:在该函数及其子函数内花费的累计时间

percall:cumtime除以调用次数

filename:lineno(function):该函数所在的文件及行号

 

posted @ 2024-04-17 09:59  新兵蛋Z  阅读(490)  评论(0)    收藏  举报