Writing Faster Python
Chris Myers, Andrew Dolgert (original author)
Cornell Center for Advanced Computing
Revisions: 6/2023, 5/2020, 8/2018, 6/2015, 5/2011 (original)
So far in this tutorial we have focused on methods for increasing the amount of compiled code that can execute during a Python program, but performance gains can also be achieved through the judicious use of the Python language itself, as well as the data structures provided as part of the Python Standard Library. In this section, we address some general strategies that can help improve the performance of Python code. While we focus here on the application of those strategies to pure Python code, to some extent they are also applicable to hybrid codes of the sort described elsewhere in this module.
Objectives
After completing this topic, you should be able to:
- Describe ways of writing faster pure Python code
- Understand how to choose the right Python container datatypes for your particular needs
- Understand the concept of lazy evaluation and how it is implemented in Python
- Describe strategies for effective memory management in Python code
Prerequisites
As this topic focuses on accelerating Python programs for scientific computing, it implicitly assumes the reader has some prior experience programming in Python, as well as working knowledge of general programming concepts. The target audience is scientists and engineers who are already programming in Python, and are interested in using Python tools and packages to improve the run time performance of the programs they are developing. If additional introductory material about Python is needed, readers can consult Introduction to Python Programming as well as the documentation on the python.org website.