本文介绍了是否有使用Python / C接口,而不是用Cython优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过写C或C ++的一些模块,使用BLAS和LAPACK延长蟒蛇和numpy的。我也希望能够分发code作为独立的C / C ++库。我想这个库使用单,双precision浮动。功能我会写一些例子是求解线性系统或加速的第一阶方法共轭梯度。有些功能需要调用从C / C ++ code Python函数。

I want to extend python and numpy by writing some modules in C or C++, using BLAS and LAPACK. I also want to be able to distribute the code as standalone C/C++ libraries. I would like this libraries to use both single and double precision float. Some examples of functions I will write are conjugate gradient for solving linear systems or accelerated first order methods. Some functions will need to call a Python function from the C/C++ code.

扮演的是一个使用Python / C API和numpy的/ C API后,我发现很多人主张用用Cython,而不是(例如见的或this 之一)。我不是用Cython的专家,但似乎对的,你仍然需要使用numpy的/ C API,知道它是如何工作的。鉴于我已经对Python的/ C API,没有关于用Cython(一些小的)的知识,我在想,如果是有意义的继续使用Python / C API时,如果使用这个API有用Cython一定的优势。在未来,我肯定会开发一些东西,不涉及数值计算,所以这个问题不仅是numpy的。其中一个我喜欢Python的/ C API的事情是,我了解了Python间preTER是如何工作的一些东西的事实。

After playing a little with the Python/C API and the Numpy/C API, I discovered that many people advocate the use of Cython instead (see for example this question or this one). I am not an expert about Cython, but it seems that for some cases, you still need to use the Numpy/C API and know how it works. Given the fact that I already have (some little) knowledge about the Python/C API and none about Cython, I was wondering if it makes sense to keep on using the Python/C API, and if using this API has some advantages over Cython. In the future, I will certainly develop some stuff not involving numerical computing, so this question is not only about numpy. One of the thing I like about the Python/C API is the fact that I learn some stuff about how the Python interpreter is working.

感谢。

推荐答案

首先,在你的问题有一点我不明白:

First, there is one point in your question I don't get:

[...]也希望能够分发code作为独立的C / C ++库。 [...]有些功能需要调用从C / C ++ code Python函数。

这是如何工作的?

接下来,为您的实际问题,当然有直接使用Python / C API的优点:

Next, as to your actual question, there are certainly advantages of using the Python/C API directly:


  • 最有可能的,你是不是写用Cython code更familar与编写C code。

  • Most likely, you are more familar with writing C code than writing Cython code.

用C编写你code给你最大程度的控制。为了从用Cython code相同的性能,相当于从C code,你必须非常小心。你不仅需要确保申报所有类型的变量,你也有充分地设置一些标志 - 只是一个例子是的。您将需要成竹在胸用Cython是如何工作的,以获得最佳的性能。

Writing your code in C gives you maximum control. To get the same performance from Cython code as from equivalent C code, you'll have to be very careful. You'll not only need to make sure to declare the types of all variables, you'll also have to set some flags adequately -- just one example is bounds checking. You will need intimate knowledge how Cython is working to get the best performance.

用Cython $ C $,c取决于Python的。它似乎并没有被写code,它也应该用Cython分布为独立C库是一个好主意

Cython code depends on Python. It does not seem to be a good idea to write code that should also be distributed as standalone C library in Cython

这篇关于是否有使用Python / C接口,而不是用Cython优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 00:17