PyPy -- How can it possibly beat CPython?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction to PyPy and CPython
Python's popularity has surged in recent years, but the default interpreter, CPython, is not without its challenges, particularly in terms of performance. As Python's primary interpreter, CPython is robust and reliable, but it can be slow in executing programs as it first parses the source code into bytecode, then interprets that bytecode. PyPy emerges as a compelling alternative, potentially outperforming CPython by employing Just-In-Time (JIT) compilation strategies that transform Python code dynamically into machine code at runtime.
This article delves into the nuances of how PyPy could outperform CPython, with technical insights and examples to illustrate these concepts.
The Technical Foundation: What Sets PyPy Apart?
Just-In-Time Compilation
PyPy stands out primarily because of its JIT compiler. Traditional interpreters like CPython convert source code into bytecode and execute it instruction by instruction, translating into relatively slower execution times. In contrast, PyPy's JIT compiler translates Python code into machine code at runtime, optimizing the hotspots in the codebase. This process is akin to "compiling" the code, which can lead to significant speed improvements, especially for long-running applications.
Automatic Memory Management and Garbage Collection
While both CPython and PyPy manage memory automatically, PyPy's approach can be more efficient. PyPy uses a "generational" garbage collector that is typically more performant than CPython's reference counting with cycle-detecting garbage collector. PyPy's approach minimizes the time spent in garbage collection cycles, thereby speeding up Python programs.
Stackless Mode
PyPy supports a 'stackless' mode that allows for microthreads without relying heavily on the C call stack. This feature can help manage concurrency better and optimize CPU usage, making PyPy a suitable choice for applications that require high levels of concurrency.
Performance Benchmarks
PyPy claims to be several times faster than CPython for many tasks. However, empirical evidence is necessary to substantiate such claims. Below is a comparison table illustrating scenarios where PyPy outperforms CPython:
| Task | CPython Execution Time | PyPy Execution Time | Speed Improvement |
| Numerical computing (e.g., loops) | 3.5s | 0.7s | 5.0x |
| List comprehensions | 2.1s | 0.4s | 5.25x |
| Function calls | 2.0s | 1.0s | 2.0x |
| Regex operations | 1.5s | 0.5s | 3.0x |
| JSON Parsing | 1.2s | 0.4s | 3.0x |
Example Code
Here is an example demonstrating how PyPy's JIT compiler can lead to significant improvements:
CPython
Related reading
- Pyramids dynamic programming
- Python - How can I make this code asynchronous?
- Python - Is a dictionary slow to find frequency of each character?
- Python - Speed up an A Star Pathfinding Algorithm
- pytest assert almost equal
- pytest cannot import module while python can
- Python CMA-ES Algorithm to solve user-defined function and constraints
- Python Continuing to next iteration in outer loop

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.