Python Optimization

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.

Filter vs List Comprehension
Avoid using lambda functions since function call overhead is high. Use list comprehension instead of filter.

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.

Tuple vs List Creation
Creating tuples are faster than lists. Use a tuple if you need an immutable sequence.

Local vs Global Variables
Accessing to local variables (or methods) are faster than accessing to globals or built-ins.

VN:F [1.9.22_1171]
Rating: 10.0/10 (6 votes cast)
Python Optimization, 10.0 out of 10 based on 6 ratings
This entry was posted in Optimization, Python. Bookmark the permalink.