发布网友 发布时间:2022-04-21 17:58
共1个回答
热心网友 时间:2022-05-10 22:54
基于普通的函数嵌套
> def log1(fn):
def _wrapper(*args, **kwargs):
start = time.clock()
result = fn(*args, **kwargs)
print("%s is invoked with time consumed: %s seconds at address %s" % (fn.__name__, str(time.time() - start), id(fn)))
return result
return _wrapper
基于@decorator
@decorator
def log(f, *args, **kwargs):
start = time.time()
result = f(*args, **kwargs)
print("%s is invoked with time consumed: %s at address %s" % (f.__name__, str(time.time() - start), id(f)))
return result
使用@functools
def log2(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
start = time.clock()
result = f(*args, **kwargs)
print("%s is invoked with time consumed: %s seconds at address %s" % (f.__name__, str(time.time() - start), id(f)))
return result
return wrapper