As most of you already know, Python is a general-purpose programming language optimized for simplicity and ease of use. While it’s a great tool for light tasks, code execution speed can soon become a major bottleneck in your programs.
In this article, we’ll discuss why Python is so slow, when compared to other programming languages. Then, we’ll see how to write a basic Rust extension for Python and compare its performance to a native Python implementation.
Why Python is slow
Before we start, I would like to point out that programming languages aren’t inherently fast or slow: their implementations are. If you want to learn about the difference between a language and its implementation, check out this article:
First of all, Python is dynamically typed, meaning that variable types are only known at runtime, and not at compile-time. While this design choice allows for more flexible code, the Python interpreter cannot make assumptions about what your variables are and their size. As a result, it cannot make optimizations like a static compiler would.
Another design choice that makes Python slower than other alternatives is the infamous GIL. The Global Interpreter Lock is a mutex lock that allows only one thread to execute at any point in time. The GIL was originally meant to guarantee thread safety but has encountered great backlash from developers of multi-threaded applications.
On top of that, Python code is executed through a virtual machine instead of running directly on the CPU. This further layer of abstraction adds a significant execution overhead, compared to statically compiled languages.
Furthermore, Python objects are internally treated as dictionaries (or hashmaps) and their attributes (properties and methods, accessed via the dot operator) aren’t usually accessed through a memory offset, but…