添加 level1(T).py
This commit is contained in:
parent
b54a99745e
commit
c1309fd8c2
38
level1(T).py
Normal file
38
level1(T).py
Normal file
@ -0,0 +1,38 @@
|
||||
import time
|
||||
|
||||
#装饰器函数
|
||||
def cal_time(func): #接收被装饰函数
|
||||
def wrapper(*args, **kwargs): #接收被装饰函数的参数
|
||||
start_time = time.time()
|
||||
result = func(*args, **kwargs)
|
||||
end_time = time.time()
|
||||
consume_time = end_time - start_time
|
||||
print(f"Function executed in {consume_time:.6f} seconds")
|
||||
return result
|
||||
return wrapper #将被装饰函数替换为wrapper函数
|
||||
|
||||
@cal_time
|
||||
|
||||
#斐波那契数列(生成器)
|
||||
def fb_sq(n):
|
||||
a, b = 0, 1
|
||||
for _ in range(n):
|
||||
a, b = b, a + b
|
||||
return a
|
||||
print((fb_sq(100000)))
|
||||
|
||||
@cal_time
|
||||
#斐波那契数列(普通循环)
|
||||
import time
|
||||
start_time = time.time()
|
||||
n = 1000 #生成斐波那契数列的项数,可修改
|
||||
result = []
|
||||
a, b = 0, 1
|
||||
for i in range(n):
|
||||
a, b = b, a + b #a是前一步的b,b是前一步的a+b
|
||||
result.append(a)
|
||||
print(result)
|
||||
end_time = time.time()
|
||||
print(f"function excuted in {end_time-start_time} s")
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user