Book Image

Mastering Python High Performance

Book Image

Mastering Python High Performance

Overview of this book

Table of Contents (15 chapters)

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)) than lists or tuples.

  • Don't reinvent the wheel: Python comes with built-in core blocks that are written in optimized C. There is no need to use hand-built alternatives, since the latter will most likely be slower. Datatypes such as lists, tuples...