In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Simplify Python to write C extension language Cython how to use, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Python is one of the most popular programming languages today because it is open source, it has a wide range of uses (such as Web programming, business applications, games, scientific programming, etc.), and it is supported by a dynamic and dedicated community. This community allows us to have such a large and diverse package in Python Package Index (PyPI) to extend and improve Python and solve inevitable problems.
In this series, we will introduce seven PyPI libraries that can help you solve common Python problems. The first is Cython, a language that makes it easier for Python to write C extensions.
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
< 2:return 1return 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 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 < 2: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.
The next time your Python code spends too much CPU time, it may cause the fan to spin. Why not see if Cython can solve the problem?
This is the answer to the question about how to simplify Python to write C extension language Cython. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.