This is the 30th day of my participation in the August Challenge

Python is one of the most popular programming languages in recent years. The advantages of Python are not to be mentioned. Those who have written Python should have experienced some of its unique features, which are really comfortable to write. It is omnipotent and has disadvantages over other languages. For example, Python can be hundreds of times slower for some applications than other languages. Python may not be the best choice for systems that require a certain amount of response time, and I have experienced Python projects that were refactored in Java because the response time was not as fast as expected. So is there a way to speed up Python? And that’s using PyPy.

What is PyPy

Python’s official interpreter is the CPython interpreter, which is developed in THE C language, so it is called CPython. CPython is by far the most widely used and best-maintained Python interpreter. There are also interpreters implemented in other languages, such as IPython: an interactive interpreter based on CPython; Jython: an interpreter implemented in the Java language; PyPy is one of them. It uses just-in-time compilation, or JIT compilation, to dynamically compile Python code to speed up execution, while being highly compatible with Cpython and having the same syntax.

Use PyPy

Installation and use

Download the zip package according to your system:Download and Install The Linux version is used as an example. After downloading, unpack:

Tar xf pypy3.7-v7.3.5-linux64.tar.bz2./pypy3.7-v7.3.5-linux64/bin/pypy3.7 Python 3.7.10 (77787b8f4c49, May 15 2021, 11:50:33) [PyPy 7.3.5 with GCC 7.3.1 20180303 (Red Hat 7.3.1-5)] on Linux Type "help", "copyright", "credits" or "license" for more information.>>>>

Copy the code

Downloading and Installing PyPy – PyPy documentation

Once installed, our Python code can be executed using PyPy. Only PyPy is not enough, some other libraries need to be installed, you need to install PyPy PIP: usepypy3 -m ensurepipInstall.

Pip3 and PIp3.7 will be in the bin directory of PyPy after the installation, and we can use them to install the other libraries we need.

Compared with retaining

Create a new test.py file and write the following code to run the test.py file using the CPython interpreter and the PyPy interpreter respectively to compare the running time.

import time

start = time.time()
for j in range(10000) :for i in range(10000):
        total = i * j

end = time.time()
print("Running time:", end - start)
Copy the code

CPython:

Python test.py Runtime: 11.55064606666565Copy the code

PyPy:

/pypy3.7-v7.3.5-linux64/bin/pypy3.7 test.py Run time: 0.250166654586792Copy the code

The results show that PyPy does run much faster than CPython.

Limitations of PyPy

Looking at this, you might be thinking, PyPy is faster than CPython and the code syntax is basically compatible, so why hasn’t it replaced CPython or become widely used? That’s because PyPy, while fast, has its limitations. Let’s talk about it briefly.

Not applicable to C extensions

PyPy is suitable for programs written in pure Python code, but as long as you use C extension libraries (such as Numpy, SciPy, etc.), it will not run faster, but will run slower than CPython. The reason is that the C extension library is based on CPython design implementation, PyPy does not fully support the C extension library, there is no way to use the C extension program optimization, which will slow down. PyPy has also been dealing with the issue of C extensions this year, some of which have been ported to PyPy, but still have some disadvantages. So if your application uses a C extension library, it is better to use the CPython interpreter.

Ecological problems

Python third-party libraries are rich and powerful, and many of these libraries are based on CPython. Pypy-compatible libraries are few and far between, so few people use them, and not many developers contribute to PyPy. It’s a vicious cycle. Simply put, ecology is bad.

PyPy is not suitable for short running programs

The longer the program runs, the more information PyPy can gather about the runtime and optimize the execution of the program. But if a program does not run for at least a few seconds, the PyPyJIT compiler will not have enough time to warm up and will run slower rather than faster.

conclusion

If your project is a business system written purely in Python, try PyPy, which may have unexpected results, and if it is useful for C extension modules, stick with CPython.

Finally, thank my girlfriend for her tolerance, understanding and support in work and life!