Member-only story
Why Python is so slow and how to speed it up
Take a look under the hood to see where Python’s bottlenecks lie
In this article we’ll discover that Python is not a bad language that is just very slow. It is optimized for the purpose it is built: easy syntax, readable code and a lot of freedom for the developer. These design choices, however, do make Python code slower than other languages like C and Java.
Understanding how Python works under the hood will show us the causes of why it’s slower. Once the causes are clear we can work our way around it. After reading this article you’ll have a clear understanding on:
- how Python is designed and works under the hood
- why these design choices affect execution speed
- how we can work around some of these bottlenecks to increase the speed of our code significantly
This article is split in three parts. In part A we take a look at how Python is designed. Then, in part B see how and why these design choices affect speed. Finally, in part C we’ll learn how to work around the bottlenecks that result from Python’s design and how we can speed up our code significantly.
Let’s go!
Part A — Python’s design
Let’s start off with a definition. Wikipedia describes Python as:
Python is an interpreted, high-level, general-purpose programming language. It is dynamically typed and garbage-collected.
Believe it or not, you’re going to understand the two sentences above after you’ve read this article. This definition provides a nice glance of Python’s design. High-level, interpreted, general-purpose, dynamic typing and the way garbage is collected take away a lot of hassle from the developer.
In the next parts we’ll go through these elements of design, explain what it means for Python’s performance and conclude with a practical example.