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
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)
- 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.
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:
Or to open python terminal, just type:
Important to note what are the CPU archetecture that pypy support and if your CPU is some of this.
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?
References and good study material
That’s all folks