Skip to content

PyPy

PyPy is an alternative implementation of Python built around a just-in-time (JIT) compiler. It’s a replacement for CPython, the reference implementation that most people install from python.org, and supports the whole core language, so most pure-Python programs run on it unchanged.

Where CPython executes your bytecode one instruction at a time, PyPy watches which parts of your program run most often and compiles those hot loops to machine code at runtime. PyPy itself is written in RPython, a restricted subset of Python.

PyPy release 7.3.23 ships two interpreters, PyPy3.11 and PyPy2.7. They match the syntax and standard library of Python 3.11 and Python 2.7, respectively.

The JIT needs a warm-up period before it pays for itself, so PyPy helps most with long-running, computation-heavy code written in pure Python. Short scripts and programs that spend their time inside C libraries see little benefit.

Example

Say you have a number-crunching loop and you want to know whether PyPy would speed it up. Time the same script under both interpreters:

Language: Python Filename: collatz.py
import platform
import time


def longest_chain(limit):
    best = 0
    for start in range(1, limit):
        number, steps = start, 0
        while number != 1:
            number = number // 2 if number % 2 == 0 else 3 * number + 1
            steps += 1
        best = max(best, steps)
    return best


started = time.perf_counter()
longest = longest_chain(300_000)
elapsed = time.perf_counter() - started
implementation = platform.python_implementation()
print(f"{implementation}: {longest} steps in {elapsed:.2f} seconds")

Then run the file once with each interpreter:

Language: Shell
$ python3 collatz.py
CPython: 442 steps in 2.22 seconds
$ pypy collatz.py
PyPy: 442 steps in 0.06 seconds

The code and the answer are identical in both runs. PyPy’s JIT notices the inner while loop repeating and compiles it to machine code, so the arithmetic stops going through the bytecode interpreter.

How big that gap is depends on how much work you hand the loop, because PyPy has to amortize its fixed warm-up cost before it comes out ahead:

Interactive diagram — enable JavaScript to view.

A tight integer loop is PyPy’s best case, though. Across its own benchmark suite, PyPy averages about three times faster than CPython 3.11.

PyPy: Faster Python With Minimal Effort

Tutorial

PyPy: Faster Python With Minimal Effort

In this tutorial, you'll learn how you can use PyPy to improve the speed of your applications. You'll see how PyPy compares with other Python implementations like CPython and learn about features that you can use to gain significant performance boosts without making changes to your code.

intermediate tools

For additional information on related topics, take a look at the following resources:


By Martin Breuss • Updated Aug. 27, 2026