Other tips and tricks

The tips mentioned earlier are some of the most common techniques to optimize a program. Some of them are Python specific (such as string concatenation or using ctypes) and others are more generic (such as memoization and lookup tables).

There are still a few more minor tips and tricks specific to Python, which we will cover here. They might not yield a significant boost of speed, but they will shed some more light into the inner workings of the language:

  • Membership testing: When trying to figure out if a value is inside a list (we use the word "list" generically here, not specifically referencing the type list), something such as "a in b", we would have better results using sets and dictionaries (with a lookup time of O(1) ...

Get Mastering Python High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.