代码:
# 装饰器计算函数运行时间
def timeCalc(func):
import time
def run(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
print("函数{0}的运行时间为: {1}".format(func.__name__, time.time() - start))
return result
return run
@timeCalc
# 函数
def testa(n):
if n == "1234":
count = 1
while True:
count += 1
if count > 10000000:
break
# 运行
if __name__ == '__main__':
testa('1234')
运行结果:
函数testa的运行时间为: 0.5667872428894043
[Finished in 0.6s]