Re:Loops in Python – Comparison and Performance
by BerislavLopac on Friday August 09, 2019 @06:23PM
(#20658679)
That is interesting. It's possible that they have done some optimisations on loops since the last time I measured it, but that was back in Python 2.6 (I think), and map was much faster than a loop (no comprehensions back then), and it was particularly obvious on very high number of input elements.
Also, from your description I suspect that the list() call over a generator might introduce extra overhead, rather than map itself...
Nope, at least not on my computer. The map solution was ~50% slower than the list comprehensions when run a list with 1 million ints, and almost twice as slow when run on a list with 100k ints.
In fact even using a for loop in the obvious way was slightly faster than map with 1 million elements (about halfway between the map and list comprehension performance wise).
Map without the last list call is however very slightly faster than the equivalent generator comprehension.