Why PyPy Runs Python Faster Than CPython: JIT Compilation Explained
The article explains how PyPy’s just‑in‑time compilation dramatically speeds up Python code—often surpassing CPython and even native C implementations—by describing the performance problem, presenting benchmark results, and detailing the differences between interpreted, ahead‑of‑time, and JIT‑compiled execution.
Guido van Rossum once said that using PyPy makes code run faster, and the article begins with this quote to introduce the performance advantage of PyPy over the standard Python interpreter.
Python is praised for enabling rapid prototyping, allowing researchers to focus on ideas rather than low‑level details, but it suffers from a major drawback: it runs much slower than compiled languages such as C or C++.
After a prototype is verified, developers often need to rewrite the code in a faster language, which adds extra work; if the original Python code could already run quickly, that extra step would be unnecessary.
PyPy solves this problem by allowing Python code to run faster—sometimes even faster than equivalent C programs—thanks to its just‑in‑time (JIT) compilation strategy.
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 benchmark shows the default CPython interpreter takes about 10 seconds, while PyPy completes the same task in only 0.22 seconds; a comparable C implementation finishes in 0.32 seconds, demonstrating PyPy’s superior speed.
The article explains that PyPy’s speed comes from JIT compilation, which translates frequently executed Python code into machine code at runtime, combining the flexibility of an interpreter with the performance of ahead‑of‑time compilation.
It contrasts this with ahead‑of‑time (AOT) compiled languages like C, C++, Swift, Haskell, and Rust, which are compiled to machine code before execution, and with interpreted languages such as Python, JavaScript, and PHP, which execute source code line‑by‑line.
Finally, the piece encourages readers to try PyPy when performance matters, noting that many users still rely on the default interpreter despite the significant speed gains offered by PyPy’s JIT approach.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.