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.10_1130]
Rating: 0.0/10 (0 votes cast)

This entry was posted in Optimization, Python. Bookmark the permalink.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">