PyPy - make Python crawl faster

Posted by davser on Sun, 16 Jan 2022 02:42:55 +0100

1. Introduction

PyPy is a faster version of python

  • The package used is exactly the same and the use method is exactly the same.
  • However, not all packages in python are supported.
  • Yes, for Python 3 7, Python2. 7.
    • Install pypy3 After 7, use the PY3 command
    • Install pypy2 After 7, use the py2 command
  • performance

2. PyPy installation configuration under Windows

  • Step 1: create a virtual environment conda create -n pypy_env
  • Step 2: activate the virtual environment_ env. You can see that under the virtual environment directory (C:\Users \ username \. conda\envs\pypy_env), there are only Scripts and conda meta folders, and the content is only conda
  • Step 3: unzip and install. Pypy official website download , after downloading, unzip it and copy the content to the virtual environment directory. If you do not need a virtual environment, neither of the first two steps is required. Just add the PyPy folder to the Path environment variable
  • Under windows, you should also ensure that VC runtime library is installed Pypy's official download page has a link to provide its installation

Then install the pip tool pypy3 -m ensurepip

After installing the package, you can directly use the command pypy3 -m pip install package name.

  • For example, PY3 - M PIP install numpy

reference resources:

3. Special package

  • numpy,imageio: no problem
  • Matplotlib: when the version is not specified, the installation always fails. When installing versions 2.2 and 3.3.1, it is successful. pypy3 -m pip install matplotlib==3.3.1
  • pandas: the installation always fails when the version is not specified. When version 1.2.0 is installed, it is successful. pypy3 -m pip install matplotlib==1.2.0
  • scipy: direct installation will fail. The installation is slow because it compiles the scipy package, which is very large

4. Test

According to the following two tests, the

  • When executing loops, PyPy is dozens of times faster than Python
  • In the use of other libraries, the speed is not necessarily faster. Even slower. Such as numpy
    • numpy is written in C + +. So its execution has been very fast. If you use pypy, it won't get faster. Similarly, SciPy and pandas should also be the test results

4.1. Test cycle

import time
t = time.time()
for i in range(10**8):
    continue
print(time.time() - t)

Test results, time consuming:

  • python3.8: 2.956s
  • PY3: 0.060s (33 times faster)

4.2. numpy test

import numpy as np
import time
t = time.time()
# row is y, column is x
row_count = 4024
col_count = row_count

center_x = col_count//2 - 0.5
center_y = row_count//2 - 0.5

x0 = np.arange(col_count)
y0 = np.arange(row_count)
x0 = x0 - center_x  # Positive to the right
y0 = center_y - y0  # Positive upward
x_grid, y_grid = np.meshgrid(x0, y0)

half_size = row_count / 2
x = x_grid.flatten() / half_size  # Converted to values between [- 1,1], unit circle
y = y_grid.flatten() / half_size

x2 = x ** 2
y2 = y ** 2
x3 = x ** 3
y3 = y ** 3
x4 = x ** 4
y4 = y ** 4

xy = x * y




Z = np.zeros([15, len(x)])

# Z0
# Fun1 = lambda x, y: 1
Z[0, :] = np.ones_like(x)
# Z1
# Fun1 = lambda x, y: y
Z[1, :] = y
# Z2
# Fun2 = lambda x, y: x
Z[2, :] = x
# Z3
# Fun3 = lambda x, y: 2*x*y
Z[3, :] = 2 * xy
# Z4
# Fun4 = lambda x, y: -1 + 2 * (x ** 2 + y ** 2)
Z[4, :] = -1 + 2 * (x2 + y2)
# Z5
# Fun5 = lambda x, y: x**2 - y**2
Z[5, :] = x2 - y2
# Z6
# Fun6 = lambda x, y: 3 * x**2 * y - y**3
Z[6, :] = 3 * x2 * y - y3
# Z7
# Fun7 = lambda x, y: -2*y + 3*y * (x**2 + y**2)  # It doesn't match the effect drawing
Z[7, :] = -2 * y + 3 * y * (x2 + y2)
# Z8
# Fun8 = lambda x, y: -2*x + 3*x * (x**2 + y**2)  # It doesn't match the effect drawing
Z[8, :] = -2 * x + 3 * x * (x2 + y2)
# Z9
# Fun9 = lambda x, y: x**3 - 3*x*y**2
Z[9, :] = x3 - 3 * x * y2
# Z10
# Fun10 = lambda x, y: 4 * x**3 * y - 4 * x * y**3
Z[10, :] = 4 * x3 * y - 4 * x * y3
# Z11
# Fun11 = lambda x, y: -6*x*y + 8*x*y * (x**2 + y**2)  # It doesn't match the effect drawing
Z[11, :] = -6 * xy + 8 * xy * (x2 + y2)
# Z12
# Fun12 = lambda x, y: 1 - 6*(x**2 + y**2) + 6*(x**2 + y**2)**2  # It doesn't match the effect drawing
Z[12, :] = 1 - 6 * (x2 + y2) + 6 * (x2 + y2)**2
# Z13
# Fun13 = lambda x, y: -3*x**2 + 3*y**2 + 4*x**4 - 4*y**4  # It doesn't match the renderings I deduced that its formula is correct, but the coefficient root sign 10 is missing
Z[13, :] = -3 * x2 + 3*y2 + 4 * x4 - 4 * y4
# Z14
# Fun14 = lambda x, y: x**4 - 6 * x**2 * y**2 + y**4
Z[14, :] = x4 - 6 * x2 * y2 + y4

print(time.time() - t)

Test results, time consuming:

  • python3.8: 6.092s
  • PY3: 6.195s (slightly slower)

Topics: Programming