SRE/level1(T).py
2025-05-19 20:27:05 +08:00

39 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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是前一步的bb是前一步的a+b
result.append(a)
print(result)
end_time = time.time()
print(f"function excuted in {end_time-start_time} s")