In terms of performance in Python, is a list-comprehension, or functions like map()
, filter()
and reduce()
faster than a for loop? Why
If you check the info on python.org, you can see this summary:
Version Time (seconds)
Basic loop 3.47
Eliminate dots 2.45
Local variable & no dots 1.79
Using map function 0.54
But you really should read the above article in details to understand the cause of the performance difference.
I also strongly suggest you should time your code by using timeit. At the end of the day, there can be a situation where, for example, you may need to break out of for
loop when a condition is met. It could potentially be faster than finding out the result by calling map
.