Python 3.5为什么map()比列表理解慢?

时间:2017-06-24 20:05:11

标签: python python-3.x dictionary list-comprehension

(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

0 个答案:

没有答案