20 lines
625 B
Python
20 lines
625 B
Python
#错误……
|
||
import time
|
||
|
||
def cal_time(func): #传递被装饰函数
|
||
def transfer(*args): #传递被装饰函数的参数
|
||
start_time = time.time()
|
||
result = func(*args)
|
||
end_time = time.time()
|
||
# consume_time = end_time - start_time
|
||
print(f"运行时间: {end_time - start_time:.3f}秒")
|
||
return result
|
||
return transfer
|
||
@cal_time
|
||
def fb_sq(n):
|
||
if n <= 1:
|
||
yield n # 递归出口,直接返回n的值
|
||
else:
|
||
# 递归获取前两项的值并相加
|
||
yield next(fb_sq(n-1)) + next(fb_sq(n-2))
|
||
print(next(fb_sq(10))) |