14.13 给你的程序做性能测试
问题
解决方案
bash % time python3 someprogram.pybash % python3 -m cProfile someprogram.py# timethis.py
import time
from functools import wraps
def timethis(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.perf_counter()
r = func(*args, **kwargs)
end = time.perf_counter()
print('{}.{} : {}'.format(func.__module__, func.__name__, end - start))
return r
return wrapper讨论
Last updated