
Increase speed by orders of magnitude and enhance code safety

As most of you already know, Python is a general-purpose programming language optimized for simplicity and ease of use. While it’s an ideal tool for light tasks, code execution speed can soon grow to be a significant bottleneck in your programs.
In this text, we’ll discuss why Python is so slow, when put next to other programming languages. Then, we’ll see methods 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 love to indicate that programming languages aren’t inherently fast or slow: their implementations are. If you must learn in regards to the difference between a language and its implementation, take a look at this text:
To start with, Python is dynamically typed, meaning that variable types are only known at runtime, and never at compile-time. While this design selection allows for more flexible code, the Python interpreter cannot make assumptions about what your variables are and their size. In consequence, it cannot make optimizations like a static compiler would.
One other design selection that makes Python slower than other alternatives is the infamous GIL. The Global Interpreter Lock is a mutex lock that permits just one thread to execute at any time limit. The GIL was originally meant to ensure 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 as an alternative of running directly on the CPU. This further layer of abstraction adds a big execution overhead, in comparison with statically compiled languages.
Moreover, Python objects are internally treated as dictionaries (or hashmaps) and their attributes (properties and methods, accessed via the dot operator) aren’t normally accessed through a memory offset, but…