(i5 ivybridge cpu windows 10)地图似乎每次出现都很慢,因为这是一个非常长的列表,它应该做得很好
import time
l = [2049 for i in range(20000000)]
def f(e):
return (e * 2) - 56
st = time.time()
l = list(map(f,l))
end = time.time()
print ("map: "+str(end-st))
l = [2049 for i in range(20000000)]
st = time.time()
l = [f(e) for e in l]
end = time.time()
print ("list comp: "+str(end-st))
打印:
map: 4.037442207336426
list comp: 3.9109268188476562