Fundamentals 5 min read

Why PyPy Can Make Python Code Run Faster Than C: A JIT Compilation Overview

The article explains how Python’s speed limitations can be overcome with PyPy’s just‑in‑time compilation, demonstrates a benchmark where PyPy outperforms both CPython and native C, and clarifies the differences between ahead‑of‑time, interpreted, and JIT execution models.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Why PyPy Can Make Python Code Run Faster Than C: A JIT Compilation Overview

For researchers, quickly turning ideas into runnable code is essential, and Python excels at this by letting developers focus on concepts rather than low‑level details.

However, Python’s major drawback is its slower execution speed compared to compiled languages like C or C++. After prototyping in Python, developers often need to rewrite code in a faster language, which adds extra work.

PyPy offers a solution by enabling Python code to run faster—sometimes even faster than equivalent C programs—through just‑in‑time (JIT) compilation.

To illustrate the speed difference, the following script adds numbers from 0 to 100,000,000 and measures execution time using the standard CPython interpreter and PyPy:

import time
from termcolor import colored

start = time.time()
number = 0
for i in range(100000000):
    number += i
print(colored("FINISHED", "green"))
print(f"Ellapsed time: {time.time() - start} s")

Running this code shows CPython takes about 10 seconds, while PyPy completes the task in roughly 0.22 seconds, even beating a comparable C implementation that runs in about 0.32 seconds.

The reason for PyPy’s speed is its JIT compiler, which translates frequently executed Python code into machine code at runtime, unlike traditional interpreters that execute source line by line.

In contrast, ahead‑of‑time (AOT) compiled languages such as C, C++, Swift, Haskell, and Rust convert source code to machine code before execution, resulting in fast runtime performance but less flexibility.

Interpreted languages like Python, JavaScript, and PHP keep source code unchanged and execute it line by line via an interpreter, offering portability and ease of development at the cost of speed.

JIT compilation combines the benefits of AOT compilation and interpretation: it compiles hot code paths to machine code on the fly, improving performance while retaining the dynamic nature of interpreted languages.

Thus, PyPy’s JIT approach provides high performance without requiring developers to rewrite Python code in a lower‑level language.

PerformanceoptimizationPythonJITPyPy
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.