In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use Cython to accelerate Python code, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
If you've ever written code in Python, waiting for some blocks of code to execute may take longer than you want. Although there are some ways to improve the efficiency of the code, it is still likely to be slower than C code. This mainly comes down to the fact that Python is a dynamic programming language that moves many things to the runtime that C is responsible for during compilation.
However, if you like writing code in Python as much as I do, and still want to speed up the code, you can consider using Cython. Although Cython itself is an independent programming language, it is easy to integrate it into your workflow, such as Jupyter Notebook. When executed, Cython converts your Python code to C, usually significantly faster
Install Cython
To be able to use Cython, you need a C compiler. Therefore, the installation process will vary depending on your current operating system. For Linux, there is usually a 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, please visit Cython's GitHub.
Once you have a C compiler, what you need to run on your terminal is:
How 1pip install Cython uses Cython
The easiest way to demonstrate the functionality of Cython is through Jupyter Notebooks. To use Cython in our notebooks, we will use the IPython magic command. The Magic command starts with a percent sign and provides additional features that enhance the workflow. Typically, there are two types of Magic commands:
The row magic is represented by a single "%" and operates on only one line of input
Cell magic is represented by two "%" and operates on multiple lines of input.
Let's start with:
First, in order to be able to use Cython, we must run:
1%load_ext Cython
Now, whenever we want to run Cython in a unit of code, we must first put the following magic command in the cell:
1%%cython
Once this is done, you can start writing Cython code.
How fast does Cython run?
How much faster Cython is compared to normal Python code actually depends on the code itself. For example, if you are running a computationally expensive loop with many variables, Cython will be much better than regular Python code. Recursive functions also make Cython much faster than Python.
Let's prove this with the Fibonacci series. Simply put, this algorithm finds the next number by adding the first two numbers. Here is what can happen in Python:
Let's make Python work:
As you can see, it took 13.3 seconds to find the 39th number in the sequence. The wall time here refers to the total time spent from the beginning of the function call to the end.
Let's define the same function in Cython.
What's going on? As you can see, we are using some unit magic to enable us to use Cython in this unit. I will soon explain the role of the "- a" option. Then, we basically use the same code as above, except now we can use static type declaration and define n as the integer type.
As you can see, by adding'- a 'after the magic command, we receive comments that show us how much Python interaction there is in the code. The goal here is to remove all the yellow lines and give them a white background. In this case, there will be no Python interaction and all the code will run in C. You can also click the "+" symbol next to each line to view the C conversion of the Python code.
How fast is this code? Let's see:
In this case, Cython is about 6.75 times faster than Python. This clearly demonstrates the ability to save time with Cython, and Cython provides the biggest improvement over regular Python code.
Additional option
If you already know the C language, Cython also allows access to C code, and the creator of Cython has not yet added a ready-made declaration to that code. For example, using the following code, you can generate a Python wrapper for the C function and add it to the module dict.
Cython demonstrates many additional features, such as parallelism, which are well described in the documentation, and you can find them here.
Conclusion
If partners sometimes have to wait too long to execute python code, cython provides a very flexible integration and efficient way to speed up code execution. Most importantly, if you are a little familiar with C, it provides a lot of functionality to further optimize the code.
After reading the above, do you have any further understanding of how to speed up Python code with Cython? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.