Photo by Cris Ovalle on Unsplash

Python Vs PyPy Speed

Bob Kfir
Bob Kfir
Mar 24 · 3 min read

How much faster does PyPy make your existing Python code?


Python is an interpreted language, which mostly runs in a runtime known as CPython. While not too bad for most tasks, CPython can be a bit slow for larger programs. So, PyPy allows you to run your existing Python code, without any changes, at a much faster speed. This is in part due to the JIT(Just in Time) compiler, which is faster after it has a bit of time to warm up. PyPy may even decrease the memory usage of memory hungry programs, although this isn’t guaranteed.

You can learn more about PyPy by visiting their website.

Installation On Ubuntu

Since Ubuntu is amazing, all it takes is one command to install:

sudo apt install pypy

After the command is done, you can run most Python programs by running:



Here’s a very non-scientific benchmark that probably has unrealistic results. Each program will be run 5 times, and the fastest run will be displayed.


My favorite benchmarks for programming languages is how long it takes to find all prime numbers up to a certain number. In Python, the fastest implementation I have so far is the following program.

import math, sys
def is_prime( num ):
if num <= 1:
return False
if num == 2:
return True
for i in range(2, int( math.sqrt( num ) + 1 ) ):
if num % i == 0:
return False
return True
for x in range( 0, int( sys.argv[1] ) + 1 ):
if is_prime( x ):
print( x )


Since I believe both PyPy and CPython cache the bytecode to some extent, I’ll run both a few times so they get a chance to cache what they need.


In order to show the difference for programs with a short runtime, I’ll first benchmark the programs by just finding all prime numbers up to 1000. It’s probably also worth mentioning that I’m redirecting all the output to /dev/null to not waste time printing.


real    0m0.012s
user 0m0.011s
sys 0m0.000s


real    0m0.040s
user 0m0.016s
sys 0m0.024s

As you can see, with short running programs, PyPy actually makes everything slower. Let’s bump it up to all prime numbers from 0 to 100,000 and see what happens.



real    0m0.274s
user 0m0.268s
sys 0m0.004s

This actually took longer than I expected. I thought it would be near-instant, but apparently not.


real    0m0.103s
user 0m0.070s
sys 0m0.033s

This is actually noticeably faster than CPython, despite not making any changes to the code.


The final test, finding all prime numbers from 0 to one million.


real    0m8.299s
user 0m8.252s
sys 0m0.020s

This actually took a rather long time. Well, I hope PyPy fares better.


real    0m1.097s
user 0m1.069s
sys 0m0.024s

Wow, that’s actually a big difference of nearly 8x. It’s not just me either, the PyPy website also says that PyPy is faster by a factor of 7.6:

It depends greatly on the type of task being performed. The geometric average of all benchmarks is 0.13 or 7.6 times faster than CPython. — PyPy

Use PyPy For Programs That Take A While

For most small programs, it doesn’t really make that much of a difference. In fact, small programs will often take longer to run with PyPy. But, if you need a program to run faster, and don’t want to change languages, PyPy definitely makes a big difference.


You can even use PyPy for Fail2Ban:

Bob Kfir’s Tech Blog

A technology blog with an emphasis on cybersecurity and privacy.

Bob Kfir

Written by

Bob Kfir

I’m a writer and a programmer. Most of what I write is about technology (often privacy and cybersecurity) and/or writing. You can learn more at

Bob Kfir’s Tech Blog

A technology blog with an emphasis on cybersecurity and privacy.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade