In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use Cython". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use Cython.
Cython is a programming language that uses Python-like syntax to write C extensions and can be called by Python. It not only has the characteristics of rapid development of Python, but also makes the code run as fast as C. at the same time, it can easily call C library.
Cython
It's interesting to use Python, but sometimes programs written with it can be slow. All runtime dynamic scheduling comes at a high cost: sometimes it is 10 times slower than equivalent code written in system languages such as C or Rust.
Migrating code to an entirely new language can be costly in terms of cost and reliability: all manual rewriting will inevitably introduce errors. Can we have both?
In order to practice optimization, we need some slow code. What could be slower than the surprise index of the Fibonacci series?
Def fib (n): if n return 1return fib (NMUI 1) + fib (NMUI 2)
Because the call to fib results in two calls again, this extremely inefficient algorithm takes a long time to execute. For example, on my new laptop, the fib (36) takes about 4.5 seconds. This 4. 5 seconds will be the benchmark for us to explore the help that Python's Cython extension can provide.
The correct way to use Cython is to integrate it into setup.py. However, using pyximport is a quick way to try. Let's put the fib code in fib.pyx and run it using Cython.
> import pyximport; pyximport.install () > import fib > fib.fib (36)
Using only Cython without modifying the code, the time spent on my laptop was reduced to about 2.5 seconds. Almost no effort is required, which reduces uptime by almost 50%. Of course, I got a good result.
Come on, we can make it faster.
Cpdef int fib (int n): if n return 1return fib (n-1) + fib (n-2)
We turn the code in fib into a function defined in cpdef and add two type comments: it takes an integer and returns an integer.
This is much faster, only about 0.05 seconds. It is so fast that I may begin to suspect that my measurement method contains noise: previously, this noise was lost in the signal.
Thank you for your reading, the above is the content of "how to use Cython", after the study of this article, I believe you have a deeper understanding of how to use Cython, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.