PyPy Series - Part 1: Introduction to PyPy
PyPy Series - Part 1: Introduction to PyPy Picture 1
This series is a series of articles around PyPy. In addition to just introducing PyPy, this series of articles also delves into the technical aspects and best practices, practical tips when using PyPy.
About PyPy
Like languages like C++, Javascript, PHP,. Python also has many different versions (implementations) to serve many purposes such as: CPython (default and most popular version), JPython (version written in Java language), PyPy (version written in Python language),.
Python implementations often aim to use the strengths and features of a certain language X in Python. For example, IronPython will support users to use .NET libraries, .NET languages in Python or JPython is a version of Python written in Java so that programmers can use both languages during development.
CPython version is the Python version written in C language and is the official and widely used version in the world (the version you download on https://www.python.org). CPython has an inherent weakness of slow performance, the page https://benchmarksgame-team.pages.debian.net/benchmarksgame/ performs solving problems/algorithms that need to be processed, calculated a lot and gives benchmark results between languages such as Swift, JS(Node.js), Dart, PHP, Python,. The results are as follows:
PyPy Series - Part 1: Introduction to PyPy Picture 2
benchmarksgame-team.pages.debian.net
We can see in the comparison table that Python (here CPython) is only faster than 2 popular languages, Perl and Ruby, while the fastest languages are C, C++, Rust.
To overcome the performance problem on CPython, PyPy was developed to serve the tasks that require high performance but still need beautiful, easy-to-understand syntax like Python. On its homepage, PyPy states that " the average performance of PyPy is 4.2 times that of CPython ". Therefore, if you want to find a solution to speed up your Python code by 4.2 times "with one command", PyPy might be a good option. In the following part of the article, we will have a basic performance evaluation between these two Python versions.
PyPy Series - Part 1: Introduction to PyPy Picture 3
pypy.org
So what is PyPy?
According to the definition on the homepage, PyPy calls itself " a fast, compliant alternative implementation of Python ". That is, a fast, "compliant" version of Python. The word "compliant" here refers to PyPy's compatibility with CPython. The word "fast" refers to PyPy's performance, the most important point and also the biggest reason to use PyPy.
Technically, PyPy is written in RPython (Restricted Python) - a small branch of Python. RPython has the same syntax as Python and is modified for higher performance. Therefore, PyPy is known as a version of Python written in Python.
Install PyPy
You can go to PyPy's Download page and download the version that matches your operating system. Once downloaded, you just need to unzip it and run the binary file.
For example here we use zsh on MacOS:
tar xf pypy3.7-v7.3.5.tar.bz2
cd pypy3.7-v7.3.5-osx64/bin
./pypy3
Python 3.7.10 (77787b8f4c49, May 19 2021, 05:28:43) [PyPy 7.3.5 with GCC Apple LLVM 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>> lst = []
>>>> lst.append(1)
>>>> lst [1]
Alternatively, if you are using MacOS you can use brew to install with one command:
brew install pypy3
Learn more about installing PyPy here: https://doc.pypy.org/en/latest/install.html
Advantages of PyPy over CPython
Advantage:
- High performance thanks to Just-in-Time (JIT) compiler. This is the biggest advantage and the biggest reason to use PyPy.
- Can write massive concurrency code thanks to Stackless feature.
- More memory optimized than CPython due to improvements in Garbage Collector mechanism.
Disadvantages:
- Poor compatibility with C extensions.
- Performance is really different from CPython in case of large, long running programs (due to the nature of the JIT compiler).
- New features are behind CPython. At the time of writing, the latest PyPy version is 3.7 while CPython is 3.9, soon to be 3.10. This is understandable because PyPy is a reimplementation of Python while CPython is the official version, so if you want to use PyPy, you should only use Python 3.6, 3.7 syntax.
These advantages and disadvantages will be analyzed in detail in the following sections.
Performance Benchmark with CPython
I will use this code to measure performance between CPython and PyPy:
import timeit
ONE_MILLION = 10 ** 6
t_empty = timeit.Timer(
"for i in range({}):n passn".format(ONE_MILLION)
)
t = timeit.Timer(
"s = 0nfor i in range({}):n s = s + inprint(s)".format(ONE_MILLION)
)
took_empty = t_empty.repeat(10, 1)
empty_best = min(took_empty)
took = t.repeat(10, 1)
best = min(took)
empty_iterate_freq = ONE_MILLION // empty_best
plus_iterate_freq = ONE_MILLION // best
print(empty_iterate_freq, plus_iterate_freq)
This code was run on a MacBook Pro 2013 2017 (CPU: 7th Generation Core i5, 7267U, 3.1GHz). Result:
- PyPy 3.7: 1542474318.0 661969327.0
PyPy 3.7 can loop 1542474318 (over 1.5 billion) times and perform 661969327 (over 666 million) times operations per second.
- CPython 3.7: 43455074.0 16236501.0
CPython 3.7 can loop 43455074 (43 million) times and perform 16236501 (16 million) operations in 1 second.
PyPy Series - Part 1: Introduction to PyPy Picture 4
Through the above performance test code, we can see that PyPy's performance is approximately 40 times faster than CPython. Why is the code only 2 to 3 lines but the performance is much better when I mentioned above that " PyPy's performance is only really different for large programs that run for a long time "? Please watch the following sections to understand better.
Conclude
As Guido van Rossum said at PyCon US 2015: " If you want your (Python) code to run faster, you can just use PyPy ".
PyPy is the fastest version of Python and can be a viable alternative to CPython in some cases. Just running your code with PyPy instead of CPython can significantly improve your program performance without any additional tweaking. But it also has some drawbacks related to compatibility and technical issues.
In Part 1 of this series, we introduced PyPy, a high-performance version of Python. In the following parts, we will delve deeper into the technical aspects and provide some tips for speeding up a real-world Python application.
You should read it
- 10 'money' consequences when Steve Jobs leaves
- Synthetic enzymes can be catalysts for artificial life
- 13 words and 10 sales secrets that certain sales people must know
- How to use conditional formatting in Microsoft Excel 2016
- Microsoft can control the operating system and remove software remotely
- Guide to advance Viettel VND 100,000 with Airtime Credit service
- 7 bluetooth headsets have the best battery life
- How to recover Windows 10 password easily
- The most beautiful, lovely and beautiful mouse set
- How to whiten yellow teeth at home quickly and effectively
- Xiaomi Mi A2 Lite has broken the screen after updating to Android 10
- What will stop you from playing mobile games for too long?