python基础
1.装饰器
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 <= right: # 候选区有值 mid = (left + right) // 2 if li[mid] == val: return mid elif li[mid] > val: # 待查找的值在mid左侧 right = mid - 1 else: # li[mid] < val 待查找的值在mid右侧 left = mid + 1 else: return None li = list(range(100000000)) linear_search(li, 38900000) binary_search(li, 38900000)
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16