Python tutorial: in Cython accelerate Python code, you can not think coming

If you've ever been written in Python code, then wait for some time blocks of code execution may take longer than you want. While there are ways to improve the efficiency of the code, but it is likely to remain slower than C code. This is mainly attributed to the fact that: Python is a dynamic programming language, it will run a lot of stuff to move to C at compile time responsible.

However, if you are like me, like writing code in Python, and still hopes to accelerate the speed of the code, then you can consider using Cython. Although Cython itself is an independent programming language, but it is easy to integrate into your workflow, such as Jupyter Notebook. When executed, Cython convert your Python code is C, often significantly speed up

Installation Cython

To be able to use Cython, you need a C compiler. Therefore, the installation process will be based on your current operating system will vary. For Linux, there is usually GNUC compiler (gncc). For Mac OS, you can download Xcode to get gncc. If you should use Windows, the installation process is a little more complicated. For more information visit Cython's GitHub.

Once you have a C compiler, you need to run on your terminal are:

1pip install Cython

How to use Cython

The easiest way to demonstrate Cython function is through Jupyter Notebooks. To use Cython in our notebooks, we will use IPython magic command. Magic command to start the percent sign, and provides some additional features that can enhance the workflow. Generally, there are two types of Magic command:

  1. Magic represented by a single row, "%", and only operates the input line
  2. Cells represented by the two magic "%", and operates on a multi-line input.

Let's start:

First, in order to be able to use Cython, we must run:

1%load_ext Cython

Now, whenever we want to run the code in Cython unit, we must first put the magic command cell:

1 %% cython

After completing these, you can start writing Cython code.

Cython run how fast?

Compared with ordinary Python code, Cython really depends on how much faster the code itself. For example, if you are running has a larger circulation computational overhead of many variables, Cython will be much better than conventional Python code. Recursive functions also make Cython much faster than Python.

Let us use the Fibonacci number to prove it. Simply put, the algorithm by the first two numbers together to find the next number. Here is the situation that may arise Python:

 

Python tutorial: in Cython accelerate Python code, you can not think coming

 

 

We work to make Python:

 

Python tutorial: in Cython accelerate Python code, you can not think coming

 

 

As you can see, to find a sequence of 39 digits it takes 13.3 seconds. wall time here refers to the total time from the beginning to the end of the function call takes.

Let us define the same function in the Cython.

 

Python tutorial: in Cython accelerate Python code, you can not think coming

 

 

How is this going? As you can see, we are using some elements of magic, so that we can use Cython in this unit. I will soon explain the role of "-a" option. Then, we basically use the same code as above, but now we can use static type declaration and n are defined as type integer.

As you can see, by adding '-a' command in the magic back, we received a number of comments that show us how much interactive Python code there. The goal here is to remove all of the yellow line, so that they have a white background. In this case, Python interaction would not exist, all the code that will run in C. You can also click on each row of "+" sign next to view the C conversion Python code.

This code How much faster? let us see:

 

Python tutorial: in Cython accelerate Python code, you can not think coming

 

 

In the present embodiment, Cython speed is about 6.75 times the Python. This clearly demonstrated the ability to use Cython time-saving, compared with conventional Python code, Cython provide the greatest improvement.

Additional options

If you already know C language, Cython also allows access to the C code, and Cython creator has not added ready-made statements to the code. For example, the following code can be generated wrapper for Python and C function module added to the dict.

 

Python tutorial: in Cython accelerate Python code, you can not think coming

 

 

Cython proved many additional features, such as parallelism, these features in the document have been well described, you can find these features here.

in conclusion

If the partners are sometimes encountered had to wait too long to run python code problem, cython provides a very flexible, integrated and efficient way to accelerate the implementation of the code. Most importantly, if you are familiar with a little bit of C, which provides a number of features to further optimize the code. Partners the advantages of good comments or suggestions you can leave a message Ha, more Python tutorials and tips will be updated from time to time for everyone!

Guess you like

Origin www.cnblogs.com/cherry-tang/p/11283857.html