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 @   新兵蛋Z  阅读(469)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示