O'Reilly logo

Python High Performance Programming by Gabriele Lanaro

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Performance tuning tips for pure Python code

As a rule of thumb, when optimizing pure Python code, you should look at what is available in the standard library. The standard library contains clever algorithms for the most common data structures such as lists, dicts, and sets. Furthermore, a lot of standard library modules are implemented in C and have fast processing times. However, it's important to always time the different solutions—the outcomes are often unpredictable.

The collections module provides extra data containers that can efficiently handle some common operations. For example, you can use deque in place of a list when you need to pop items from the start and append new items at the end. The collections module also includes a Counter ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required