Run Your Python Code as Fast as C – Twins Daily

Asa PhD researcher, it is crucial for my job to quickly code up an idea to see if it works or not. Python is an excellent tool enabling just that. It allows for focusing on the idea itself and not be bothered with boilerplate code and other tedious things.
However, Python comes with a major drawback: It is much slower than compiled languages like C or C++. So, what do we do after we tested an idea by building a Python prototype and now we want to turn it into a fast and performant tool? Most often than not, we end up doing twice the work and manually converting the Python code into C. Wouldn’t it be great, if our Python prototype itself could run faster? All the time spent on implementing everything twice could be used for something more meaningful and fun.
Fortunately, I stumbled upon a solution to the problem: PyPy, a fast alternative to Python able to run Python code faster than C.
To illustrate how much of a speed up PyPy can provide, I ran both the default Python interpreter and PyPy on the following example:
Basically, the script adds up all the integers between 0 and 100,000,000 in a loop and prints a message and the script’s runtime whenever it is finished.
Although not a scientific evaluation, the example is still mind-blowing. In comparison to the default Python interpreter, which needs roughly 10 seconds, PyPy finishes its execution after just over 0.22 seconds! Also, note that we can just feed our Python code to PyPy without any changes. The result becomes even more impressive when you compare it to C, the master of speed. On my computer, the equivalent implementation in C takes 0.32 seconds. So PyPy can even beat C.
“If you want your code to run faster, you should probably just use PyPy.”
— Guido van Rossum (creator of Python)
Why is PyPy soFast?
This is the question you are probably asking yourself if you are stumbling over PyPy for the first time, and rightfully so. It seems like magic in the beginning. We are running the exact same code and with PyPy get a massive speedup seemingly for free.
Though the code is exactly the same, how the code is executed couldn’t be more different. The secret behind PyPy’s performance boost is just-in-time compilation, JIT compilation for short.
But let’s take it slowly.
Ahead-Of-Time Compilation
Programming languages like C, C++ but also Swift, Haskell, Rust, and many more are compiled ahead-of-time. This means that after you wrote some code in those languages, you hit a button and a compiler converts the source code into machine-readable code, readable by one specific computer architecture. Whenever the program is executed, your original source code is long gone. All that is executed is the machine code.
Ahead-of-Time Compilation converts source files to machine code.
Language Interpretation
Python, JavaScript, PHP, and similar languages take a different approach. They are interpreted. In comparison to converting the source code to machine code, the source stays intact. Every time the program is run an interpreter “looks at” the code line by line and runs it for us.
An interpreter runs a program line by line.
In the case of JavaScript, an interpreter is built into every web browser. The standard Python interpreter is called CPython. However, it is very important to distinguish between Python the language, and the thing that runs our code, CPython. That’s because we can have completely different tools all with the capability of running Python code. Here is where PyPy enters the picture.
Just-In-Time Compilation
PyPy is an alternative implementation of Python exploiting just-in-time compilation. What’s happening is that PyPy starts like an interpreter running our Python code directly from the source files. However, instead of running the code line by line, PyPy compiles parts of the code to machine code just before it executes them, just in time so to speak.
JIT compilation combines ahead-of-time compilation and interpretation.
In this sense, JIT compilation is a combination between interpretation and ahead-of-time compilation. We gain the performance boost of ahead-of-time compilation and the flexibility and cross-platform availability of interpreted languages.
How Can You Benefit from PyPy?
Now that we understand how PyPy achieves the astonishing performance increase, we want to use it. PyPy is freely available at pypy.org and easy to install. Additionally to the tool itself, the site contains plenty of tips and tricks on fine-tuning your python program to further increase the performance. As PyPy is just an alternative implementation of Python, most of the time it just works out-of-the-box without any changes to your Python project. It is fully compatible with the web framework Django, the scientific computing package Numpy and numerous other packages. It saved me countless hours of reimplementing my prototypes in C and I wouldn’t want to miss it from my programming tool kit.
Found this story interesting? Follow me (Marcel) on Medium. Feel free to ♡ this article if you liked it and to reach out to me with private questions and comments on LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *

www.000webhost.com