经常遇到耗时分析、资源利用率分析等,最近手搓了一份统计函数的耗时和CPU使用率的Python装饰器,纯个人的自定义用法,后续再改进。
import time
import psutil
import numpy as np
def profile(func):
def wrapper(*args, **kwargs):
result = None
elapsed, cpu_percentage, cpu_nozero_times = 0.0, 0.0, 0
psp = psutil.Process(os.getpid())
t0 = time.time()
psp.cpu_percent() # last call
result = func(*args, **kwargs)
cpu_percent = psp

最低0.47元/天 解锁文章
3420

被折叠的 条评论
为什么被折叠?



