[Introduction to Python Tutorial] A small operation can speed up your Python code by more than 6 times

In our previous articles, we have talked about several methods of calculating the Fibonacci sequence, of which the recursive-based method is the slowest, such as calculating the value of the 40th item, which takes more than 36 seconds. As shown below:

[External link image transfer failed, the origin site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-ZfFyPJng-1635764828521) (https://upload-images.jianshu.io/upload_images/25002343-eb9e7249496ff9b8?imageMogr2 /auto-orient/strip%7CimageView2/2/w/1240)]

To improve the operation speed, the fundamental way is of course to improve the algorithm. However, the improvement of the algorithm is a process of long-term accumulation and inspiration. What we are going to talk about today is a no-brainer and immediate method - compiling Python code to C language code. Speed ​​up the computing process with the efficiency of the C language.

This process looks complicated, but you don't actually need to write a single line of C code. All you need to do is compile Python code to C code using a library called Cython.

First let's install Cython, just like installing a normal third-party library:

After the installation is complete, we separately write the function to calculate the Fibonacci sequence:

Very simple recursive writing.

Then the key comes, we want to save this file as . Note the suffix is ​​. As shown below:

image

Then we create a file with the following contents:

As shown below:

image

The role of this file is to call Cython functions to convert Python code to C code.

Next, start compiling the code and execute the following command:

My Python is Python3.7, so after the operation is completed, a file will be generated. If your Python is 3.8, this file name may be. You can change the name of this file, for example, to .

There is also a file called . But you don't need to open this file, because it has more than 3200 lines. And you can even delete it directly. Only this file is really useful.

All you need to do is call your function directly. We create another file with the following content:

The operation effect is shown in the following figure:

image

It only takes less than 6 seconds to calculate the 40th item of the Fibonacci sequence, which takes less than 1/6 of the original Python version. (The specific multiples vary on different computers, but the effect is very obvious)

Using Cython, you can not only improve the running speed of the program, but also convert your core code into files, preventing others from decompiling and seeing your code.
For more information about Cython, please read its official documentation:

https://cython.readthedocs.io/

Some students may ask, since there is a file under the current folder, why do we not import the code of the Python version from this file when we execute it?

This is because the module will only be imported from the file with the suffix ///, and will not be searched in the file.

Recommended articles in the past:

Can you really learn Python by yourself? If
you want to easily get started with Python programming, you must read these 10 classic cases, and you can find a job after
learning Python learning: quickly build a python environment

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326813328&siteId=291194637