python基础
2021-02-03 13:17
标签:__name__ arch python return port rgs div bin int python基础 标签:__name__ arch python return port rgs div bin int 原文地址:https://www.cnblogs.com/shengjunqiye/p/12801105.html1.装饰器
import time
#装饰器,计算代码运行时间
def cal_time(func):
def wrapper(*args, **kwargs):
t1 = time.time()
result = func(*args, **kwargs)
t2 = time.time()
# print(t1)
# print(t2)
print("%s running time: %s secs." % (func.__name__, t2 - t1))
return result
return wrapper
@cal_time
def linear_search(li, val):
for ind, v in enumerate(li):
if v == val:
return ind
else:
return None
@cal_time
def binary_search(li, val):
left = 0
right = len(li) - 1
while left # 候选区有值
mid = (left + right) // 2
if li[mid] == val:
return mid
elif li[mid] > val: # 待查找的值在mid左侧
right = mid - 1
else: # li[mid]
left = mid + 1
else:
return None
li = list(range(100000000))
linear_search(li, 38900000)
binary_search(li, 38900000)
上一篇:Java反射
下一篇:Python安装与环境变量的配置