Ever need more performance from that amazing python program you wrote?
Or even if you just want to wait less while your Python programs does your math homework for you, PyPy is the solution.
What is PyPy?
I’ll let their documentation do the explaining:
PyPy is a fast, compliant alternative implementation of the Python language (2.7.13 and 3.5.3). It has several advantages and distinct features:
Speed: thanks to its Just-in-Time compiler, Python programs often run faster on PyPy.
Memory usage: memory-hungry Python programs … might end up taking less space than they do in CPython.
Compatibility: PyPy is highly compatible with existing python code. It supports cffi and can run popular python libraries like twisted and django.
Stackless: PyPy comes by default with support for stackless mode, providing micro-threads for massive concurrency.
How do I use this?
If you’re on Ubuntu, it’s really easy. Just run:
sudo apt install pypy
and you’re done. Now, instead of
or, change the shebang to:
I created a simple factoring program in Python, which I didn’t spend a lot of time optimizing. With the standard,
time python factoring.py 900 60 30 and 30 real 0m0.944s user 0m0.827s sys 0m0.011s
now, with pypy:
time pypy factoring.py 900 60 30 and 30 real 0m0.498s user 0m0.076s sys 0m0.092s
as you can see, the time is nearly cut in half by simply switching to
CPython, without changing any code. PyPy is designed to be compatible with existing python code, and supports many of the highly-used and/or standard python libraries.
There are a few problems with PyPy, however. The biggest one is that in order to enjoy faster run times, your users will also need to have PyPy installed. While this won’t be a problem for many, for people using systems they don’t have admin on (a Mac, for example), they’ll be stuck with the slower CPython. Also, while most programs are compatible, and I have not noticed anything it can’t run, there will likely be at least a few programs that PyPy can’t run.