Fast executing efficient code is always desirable, especially for programs that will operate on web-scale data. Small performance gains will lead to big time improvements due to massive size of the input. Here I want to share python optimization lessons I learned while coding the create index program of the previous post. I assume that you are familiar with python basics such as list comprehensions, map, filter, etc.
Map vs List Comprehension
If you’re going to use lambda functions, use list comprehension instead of map.
If you’re going to use built-in functions, use map instead of list comprehension.
However, both are faster than simple for-loop.
Range vs Xrange
Use xrange instead of range. Because range creates the whole list in memory, while xrange is a generator objects and returns values on-demand.
The difference is even more noticable with larger numbers.