def method1():
alist = []
i = 0
while i < 10000:
alist.append(i)
i = i + 1
alist = []
i = 0
while i < 10000:
alist.append(i)
i = i + 1
blist = []
x = 0
while x < 500:
blist.append(x)
x = x + 1
x = 0
while x < 500:
blist.append(x)
x = x + 1
y = 0
while y < 2000:
alist.extend(blist)
y = y + 1
return alist
while y < 2000:
alist.extend(blist)
y = y + 1
return alist
def method2():
alist = []
i = 0
while i < 10000:
alist.append(i)
i = i + 1
alist = []
i = 0
while i < 10000:
alist.append(i)
i = i + 1
blist = []
x = 0
while x < 500:
blist.append(x)
x = x + 1
x = 0
while x < 500:
blist.append(x)
x = x + 1
y = 0
while y < 2000:
alist = alist + blist
y = y + 1
return alist
while y < 2000:
alist = alist + blist
y = y + 1
return alist
import time
startTime1 = time.time()
method1()
print '方法一消耗的时间是:%s 秒' % round((time.time() - startTime1), 3)
startTime1 = time.time()
method1()
print '方法一消耗的时间是:%s 秒' % round((time.time() - startTime1), 3)
print 50*"="
import time
startTime2 = time.time()
method2()
print '方法二消耗的时间是:%s 秒' % round((time.time() - startTime2), 3)
startTime2 = time.time()
method2()
print '方法二消耗的时间是:%s 秒' % round((time.time() - startTime2), 3)
测试结果:
方法一消耗的时间是:0.047 秒
==================================================
方法二消耗的时间是:12.871 秒
==================================================
方法二消耗的时间是:12.871 秒
#===============================================================================
# 测试结论:当做列表操作的时候,建议用extend()函数代替+
#===============================================================================
# 测试结论:当做列表操作的时候,建议用extend()函数代替+
#===============================================================================