PyPy, a faster Python

Guilherme Euzébio
4 min readApr 10, 2021

By default Python has CPython under the hood, wait, calm down, what is CPython? Maybe many of you don’t know what it is. Basically CPython is the reference implementation of Python. It is the responsible to interpreter your Python code and run it. CPython is an interpreter and a compile (it compiles Python code into bytecode) of Python code. Also it is where we have the grammar of Python language.

Ok, but what is PyPy and where it comes? PyPy is an alternative implementation to CPython that follows the official Python language specification and does almost the same job of CPython, but with some interesting differences. PyPy is a just in time (JIT) compiler, it uses a technique known as meta-tracing, which is responsible to transform an interpreter into a JIT compiler.

In summary, when you execute your Python code you are probably using the standard way, which means that under the hood there is CPython, but you can do it in a different way, you can run your Python code with PyPy under the hood or also with others implementations, but other implementation is topic for other article. Ok, but why should I use something different than the standard implementation?

Because maybe you need speed, which means PyPy can be the solution to you. In the words of Python creator, Guido van Rossum, “If you want your code to run faster, you should probably just use PyPy.

Animated to use PyPy and have a faster python project? Attention to some details

Compatibility

Most Python lib runs on PyPy except for those that depends on CPython extensions, which either does not work or incurs some overhead when run in PyPy.

In this link we have some of the most commom lib in Python and if it’s compatible with PyPy or not.

When not to use PyPy?

When you don’t need very fast performance

PyPy is not up-to-date with Python versions

PyPy has performance issues when you’re using c-extensions

PyPy only has CPU architecture support to:

  • x86 (IA-32) and x86_64
  • ARM platforms (ARMv6 or ARMv7, with VFPv3)
  • AArch64
  • PowerPC 64bit, both little and big endian
  • System Z (s390x)

Memory increase over execution time

Be careful and pay attation to this. PyPy garbage collector is different from CPython. There are some reports where developers have problem with memory increase, so before decid to use PyPy study it’s garbage collector and the bast way to configure and use it in your application.

Some benchmarks

According to PyPy official site the speed difference between CPython and PyPy “depends greatly on the type of task being performed. The geometric average of all benchmarks is 0.23 or 4.4 times faster than cpython”

If you want to know more about in detail I really recommend this amazing post: Which is the fastest version of Python?

How to install and use pypy?

If you wish to test pypy and is using ubuntu, then just:

sudo apt-get install -y pypy3

To use it, type in terminal:

pypy3 file_you_wish_to_run.py

Or to open python terminal, just type:

pypy3

Important to note what are the CPU archetecture that pypy support and if your CPU is some of this.

Remember…

With great power comes great responsibility.

PyPy is not for every project, you need to understand your needs and if it makes sense to your scenario.

If you never felt a need to speed improvement in your project, probably you don’t need PyPy, but if you felt this speed need, then you should test if PyPy can be the solution to you.

And don’t forget, make performance tests with PyPy and CPython, monitor the consumption of resources and if the the gains are worthwhile in your project.

Did you already know PyPy? Did you already work in a project that uses PyPy? Did you notice speed difference?

Comment below.

References and good study material

https://www.pypy.org/

https://www.youtube.com/watch?v=zQVytExlnEk

https://www.youtube.com/watch?v=1n9KMqssn54

https://hackernoon.com/which-is-the-fastest-version-of-python-2ae7c61a6b2b

https://en.wikipedia.org/wiki/PyPy

https://www.youtube.com/watch?v=sS7ACBXWOm0

https://speed.pypy.org/

https://realpython.com/pypy-faster-python/

https://towardsdatascience.com/getting-started-with-pypy-ef4ba5cb431c

https://morepypy.blogspot.com/2019/01/pypy-for-low-latency-systems.html

That’s all folks

--

--