Fundamentals 5 min read

Why PyPy Runs Python Code Faster Than C: Understanding JIT Compilation

The article explains how PyPy’s just‑in‑time compilation dramatically speeds up Python execution, often surpassing native C performance, by contrasting interpreted, ahead‑of‑time compiled, and JIT approaches and presenting a benchmark that shows PyPy completing a 100 million‑iteration loop in 0.22 seconds versus 10 seconds for CPython.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Why PyPy Runs Python Code Faster Than C: Understanding JIT Compilation

Rapid prototyping is essential for researchers, and Python enables quick idea implementation without worrying about low‑level details.

However, Python’s execution speed is much slower than compiled languages such as C or C++, and manually converting Python code to C adds extra work.

PyPy addresses this problem by allowing Python code to run faster, often surpassing even native C performance.

<code>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")
</code>

The benchmark shows the default CPython interpreter takes about 10 seconds for the loop, while PyPy completes it in 0.22 seconds, beating a comparable C implementation that needs 0.32 seconds.

PyPy’s speed comes from Just‑In‑Time (JIT) compilation, which combines the flexibility of interpreted languages with the performance of ahead‑of‑time (AOT) compiled code.

In AOT compilation (used by C, C++, Swift, Rust, etc.), source code is transformed into machine code before execution, so the processor runs native instructions directly.

Interpreted languages like Python, JavaScript, and PHP keep source code unchanged; the interpreter reads and executes each line at runtime.

PyPy’s JIT first translates frequently executed Python code into machine code, then runs that compiled version, achieving the speed of AOT while retaining the dynamism of interpretation.

PerformancePythonprogrammingJITPyPy
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.