Fall in love with the python series-python performance (1): pypy practice

As an interpreted language, python's execution efficiency has been criticized, and the speed is tens to hundreds of times slower than c

Here mainly talks about pypy

It is an interpreter, the default interpreter of python we installed is Cpython

For example, we usually use python commands:

root@root:/opt# python
Python 2.7.16 (default, Oct  7 2019, 17:36:04) 
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Such a command is Cpython. The above is very clear. What about gcc:

 The function of the interpreter is to convert our written python code (the so-called source code) into machine code, so that our machine can execute it

And pypy mainly uses jit technology, which can be compiled immediately

The biggest problem of Cpython as the interpreter is to execute line by line, which will be very slow, so I learned to use a way like jvm

Okay, let's see how to use pypy

We can think of pypy as another python environment

 

1. Install pypy

1. Download the compressed package: https://www.pypy.org/download.html

Choose to download according to your own system, here I am choosing py3.7 for linux

2. Unzip the installation package

tar xf pypy-x.y.z.tar.bz2

The decompressed content is under /opt/ 

3. Set environment variables

Add the following to both /etc/profile and ~/.bashrc

export PATH=/opt/pypy3.7-v7.3.2-linux64/bin:$PATH

Refresh the content configuration to take effect:

source /etc/profile
source ~/.bashrc

4. Try to see if it can run

root@root:/opt/pypy3.7-v7.3.2-linux64# pypy
Python 3.7.4 (87875bf2dfd8, Sep 24 2020, 07:26:36)
[PyPy 7.3.2-alpha0 with GCC 7.3.1 20180303 (Red Hat 7.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>>

See that the current interpreter is no longer gcc:

 

 2. Install the required modules

We know that we basically have pip to install third-party packages

Pypy defaults to a new environment, so the previously installed package cannot be used and needs to be reinstalled. If you don't believe it, you can try:

cpython:

root@root:/opt/pypy3.7-v7.3.2-linux64# python3 -c "import numpy"
root@root:/opt/pypy3.7-v7.3.2-linux64# 

The execution is successful, indicating that numpy is not installed

pypy :

root@root:/opt/pypy3.7-v7.3.2-linux64# pypy -c "import numpy"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'

Found that numpy is not installed, so we need to install numpy manually

Need to install pip before installation

pypy -m ensurepip

After this command is successful, pip can be installed, and it is best to update it after installation

pypy -mpip install -U pip wheel

Now we can install the installation package using pip

Use the command:

pypy -mpip install 安装包名

For example, our numpy should use:

pypy -mpip install  numpy

Attentive readers should notice that when the previous python was used for pip, it was installed like this:

pip install installation package name or python3 -mpip install installation package name

In the final analysis, we run the pypy command directly instead of the python3 command

However, it is worth noting that the installation speed of pypy -mpip install will be much slower than the previous cpython environment. I don't know if it is because of my computer.

 

Three. Compare cpython speed

The example is used on "Python Ninja Cheats" 7.4

1. Create a new file named time.py and write the following content:

import math
TIMES = 10000000
a = 1
b = 1
for i in range(TIMES):
    c = math.sqrt(math.pow(a, 2) + math.pow(b, 2))
    a += 1
    b += 2

2. Create a new file named time2.py and write the following content:

import math
TIMES = 10000000
a = 1
b = 1
def calcMath(i, a, b):
    return math.sqrt(math.pow(a, 2) + math.pow(b, 2))
for i in range(TIMES):
    c = calcMath(i, a, b)
    a += 1
    b += 2

We can see that there is just one more function for reuse

Then we are left with the results:

root@root:/opt/pypy3.7-v7.3.2-linux64# time pypy time.py

real	0m1.049s
user	0m0.992s
sys	0m0.028s
root@root:/opt/pypy3.7-v7.3.2-linux64# time pypy time2.py

real	0m1.007s
user	0m0.962s
sys	0m0.020s

root@root:/opt/pypy3.7-v7.3.2-linux64# time python3 time2.py

real	0m6.578s
user	0m6.536s
sys	0m0.008s
root@root:/opt/pypy3.7-v7.3.2-linux64# time python3 time.py

real	0m5.682s
user	0m5.636s
sys	0m0.008s

It can be seen that pypy has speeded up a lot, and pypy has no loss in using functions, while the original cpython will lose a lot

Of course, you need to see the speed gap between pypy and cpython when the amount of data is large, and it is possible that it will be slower when the amount of data is small.

For example, this code:

import time
start=time.time()
ls1=[i for i in range(1000)]
ls2=[i for i in range(1000)]
ls=ls2+ls1
end=time.time()
print(end-start)

"""
运行结果:
root@root:/opt/pypy3.7-v7.3.2-linux64# python3 test.py
7.081031799316406e-05
root@root:/opt/pypy3.7-v7.3.2-linux64# pypy test.py
0.0001647472381591797
"""

reference:

[1].https://doc.pypy.org/en/latest/install.html

[2]. "Python Ninja Cheats"

Guess you like

Origin blog.csdn.net/zhou_438/article/details/109178958