python装饰器计算函数执行时间
程序员文章站
2022-07-09 19:05:44
import time import logging from functools import wraps import datetime log = logging.getLogger(__name__) def count_time(func): @wraps(func) def int_ti ......
import time import logging from functools import wraps import datetime log = logging.getlogger(__name__) def count_time(func): @wraps(func) def int_time(*args, **kwargs): start_time = time.time() # 程序开始时间 log.info("task begin at: %s" %(datetime.datetime.fromtimestamp(start_time))) func() end_time = time.time() # 程序结束时间 log.info("task end at: %s" %(datetime.datetime.fromtimestamp(end_time))) total_time = end_time - start_time log.info('程序共计%s秒' % total_time)