Python_Day5_性能优化&推导式
循环代码优化
遵循原则
减少内部循环减少内部循环计算量,可以尽可能向外提使用局部变量,查询较快
import time
#时间0.23
start = time.time()
for i in range (1000):
result = []
for j in range(1000):
result.append(i*1000 + j*1000)
end = time.time()
print("耗时:{0}".format((end-start)))
#内部循环外提,时间0.16
start1 = time.time()
for i in range (1000):
result = []
k = i*1000
for j in range(1000):
result.append(k + j*1000)
end1 = time.t
共有 0 条评论