In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use Cython to write faster C extensions for Python. The content is concise and easy to understand. It will definitely make your eyes shine. I hope you can learn something from the detailed introduction of this article.
Learn how to solve common Python problems in our series of seven PyPI libraries.
Python is one of the most popular programming languages in use today because: it is open source, it has a wide range of uses (e.g. Web programming, business applications, games, scientific programming, etc.), and it has a vibrant and dedicated community supporting it. This community allows us to have such a large and diverse collection of packages in the Python Package Index (PyPI) to extend and improve Python and solve inevitable problems.
In this series, we'll cover seven PyPI libraries that can help you solve common Python problems. The first is Cython, a simplified Python language for writing C extensions.
Cython
Python is fun to use, but sometimes programs written in it can be slow. All run-time 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 a completely new language can come at a huge cost in terms of cost and reliability: all manual rewriting inevitably introduces errors. Can we have both?
To practice optimization, we need some slow code. What could be slower than the fibonacci surprise index?
def fib(n): if n
< 2: return 1 return fib(n-1) + fib(n-2) 由于对 fib 的调用会导致两次再次调用,因此这种效率极低的算法需要很长时间才能执行。例如,在我的新笔记本电脑上,fib(36) 需要大约 4.5 秒。这个 4.5 秒会成为我们探索 Python 的 Cython 扩展能提供的帮助的基准。 使用 Cython 的正确方法是将其集成到 setup.py 中。然而,使用 pyximport 可以快速地进行尝试。让我们将 fib 代码放在 fib.pyx 中并使用 Cython 运行它。 >>> import pyximport; pyximport.install()>>> import fib>>> fib.fib(36)
Using only Cython without modifying the code, the algorithm reduced the time it took on my laptop to about 2.5 seconds. Almost no effort is required, which reduces uptime by almost 50%. And, of course, we got a good result.
Come on, we can make it faster.
cpdef int fib(int n): if n < 2: return 1 return fib(n - 1) + fib(n - 2)
We changed the code in fib to a function defined in cpdef and added two type comments: it accepts an integer and returns an integer.
This is much faster, 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.
The next time your Python code takes too much CPU time, it might cause fans to spin, so why not see if Cython can solve the problem?
That's how to write faster C extensions for Python using Cython. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.
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.