The Python programming language is an interface that can be implemented in many ways. Some examples include CPython which uses the C language, Jython that is implemented using Java, and so on.

Despite being the most popular, CPython is not the fastest. PyPy is an alternate Python implementation that is both compliant and fast. PyPy depends on just-in-time (JIT) compilation that dramatically reduces the execution time for long-running operations.

In this tutorial, PyPy will be introduced for beginners to highlight how it is different from CPython. We’ll also cover its advantages and limitations. Then we’ll take a look at how to download and use PyPy to execute a simple Python script. PyPy supports hundreds of Python libraries, including NumPy.

Specifically, this tutorial covers the following:

  • A quick overview of CPython
  • Introduction to PyPy and its features
  • PyPy limitations
  • Running PyPy on Ubuntu
  • Execution time of PyPy vs CPython

Let’s get started.

A Quick Overview of CPython

Before discussing PyPy, it is important to know how CPython works. My previous tutorial titled Boosting Python Scripts With Cython gave a longer introduction to how CPython works, but it won’t hurt to have a quick recap here about the important points. Below you can see a visualization of the execution pipeline of a Python script implemented using CPython.

Image for post

Given a Python .py script, the source code is first compiled using the CPython compiler into bytecode. The bytecode is generated and saved in a file with a .pyc extension. The bytecode is then executed using the CPython interpreter within a virtual environment.

There are benefits to using the compiler to convert the source code into bytecode. If no compiler is used, then the interpreter will work directly on the source code by translating it line by line into machine code. The disadvantage of doing this is that some processes have to be applied for translating each line of source code into machine code, and such processes will be repeated for each line. For example, syntax analysis will be applied to each line independently from the other lines, and thus the interpreter takes a lot of time to translate the code. The compiler solves this issue as it is able to process all of the code at once, and thus syntax analysis will be applied only once rather than to each line of code. The generated bytecode from the compiler will thus be interpreted easily. Note that compiling the entire source code might not be helpful in some cases, and we’ll see a clear example of this when discussing PyPy.

After the bytecode is generated, it is executed by the interpreter running in the virtual machine. The virtual environment is beneficial, as it isolates the CPython bytecode from the machine, and thus makes Python cross-platform.

Unfortunately, just using a compiler to generate the bytecode is not enough to speed up the execution of CPython. The interpreter works by translating the code, each time it is executed, into machine code. Thus, if a line L takes X seconds to be executed, then executing it 10 times will have a cost of X*10 seconds. For long-running operations, this is too costly in its execution time.

Based on the drawbacks of CPython, let’s now take a look at PyPy.

Introduction to PyPy and its Features

PyPy is a Python implementation similar to CPython that is both compliant and fast. “Compliant” means that PyPy is compatible with CPython, as you can use nearly all CPython syntax in PyPy. There are some compatibility differences, as mentioned here. The most powerful advantage of PyPy is its speed. PyPy is much faster than CPython; we’ll see tests later on where PyPy performs about 7 times faster. In some cases it might even be tens or hundreds of times faster than CPython. So how does PyPy achieve its speed?

#cpython #python #pypy #machine-learning #developer

Getting Started with PyPy
1.90 GEEK