Some performance considerations
There are usually multiple ways of achieving the same result. We can use any combination of map()
, zip()
, and filter()
, or choose to go with a comprehension or a generator. We may even decide to go with for
loops. Readability is often a factor in choosing between these approaches. List comprehensions or generator expressions are often easier to read than complex combinations of map()
and filter()
. For more complicated operations, generator functions or for
loops are often better.
Besides readability concerns, however, we must also consider performance when deciding which approach to use. There are two factors that need to be considered when comparing the performance of different implementations: space
and time
.
Space refers to the amount of memory that your data structures are going to use. The best way to choose is to ask yourself if you really need a list (or tuple), or whether a generator would work instead.
If the answer is yes to the...