import time import random # python 列表 和元组通过千万级别的数据访问,对比耗时时间,元组访问速度不一定比列表快,还是列表略快过元组。 def ListTime(): print('正在计算访问列表元素耗时时间') l = [i for i in range(10000000)] t1 = time.time() for _ in range(10000000): l[random.randint(0, 9999999)] t2 = time.time() h = t2-t1 print(f"列表元素访问耗时:{h}s") def TupleTime(): print('正在计算访问元组元素耗时时间') l = tuple(i for i in range(10000000)) t1 = time.time() for _ in range(10000000): l[random.randint(0, 9999999)] t2 = time.time() h = t2 - t1 print(f"元组元素访问耗时:{h}s") if __name__ == '__main__': ListTime() TupleTime()
标签:__,10000000,列表,访问速度,耗时,62,time,元组 From: https://www.cnblogs.com/chenzhi2023/p/18152465