C(或 C++)与 Python 的接口有哪些不同的选项?
我知道有很多方法可以将 C 函数连接到 Python:Python C API、scipy.weave, ctypes,pyrex/cython,SWIG, Boost.Python、Psyco...它们各自最好的是什么为了?为什么我应该使用给定的方法而不是其他方法?当我需要在 Python 和 C 之间选择绑定时应该考虑什么?
我知道一些关于此的讨论,但它们似乎都不完整......
- http://wiki.cython.org/SWIG< /a>
- http://sage.math.washington.edu/tmp/sage-2.8.12.alpha0/doc/prog/node35.html
我知道StackOverflow上的一些问题也相关。例如:
- 关于连接现有 C 库
- < a href="https://stackoverflow.com/questions/5720272/are-there-advantages-to-use-the-python-c-interface-instead-of-cython">C API 与 Cython
I know there are many ways to interface C function into Python: the Python C API, scipy.weave, ctypes, pyrex/cython, SWIG, Boost.Python, Psyco... What are each of them best for? Why should I use a given method instead of others? What should be considered when I need to choose a binding between Python and C?
I know some discussions about that, but they all seems incomplete...
- http://wiki.cython.org/SWIG
- http://sage.math.washington.edu/tmp/sage-2.8.12.alpha0/doc/prog/node35.html
I know that some questions on StackOverflow are related too. For example:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管我曾在某一点或另一点研究过所有这些方法,但我还没有使用过所有这些方法...
Python C API:用于编写可编译为可在 Python 中导入的 python 模块的 C 代码。或者编写一个充当“粘合”代码来与某些 C 库交互的 Python 模块。
scipy.weave:允许您将 C 代码片段插入到 Python 代码中,如果您使用 NumPy 和 SciPy 进行数字工作,请查看此内容。 C 代码将作为字符串,例如 weave.inline('printf("%s", foo)') 。
ctypes:一个 Python 模块,允许您从 Python 代码调用 C 代码。您基本上导入共享库,然后调用其 API。需要做一些工作来整理这些调用中的数据和从这些调用中取出的数据。如果您正在考虑使用您或其他人编写的现有 C 库,我会从这里开始。
pyrex/cython:允许您编写 Python 代码(使用一些特殊语法),这些代码将生成为 C 代码(可以作为 Python 模块导入),并且显然比原来的运行速度更快通过Python解释器运行。这有点像“Python C API”路线,只是它为您生成 C 代码。如果您有一些代码块是您的瓶颈并且速度非常慢,那么这很有用。使用 cython 重写该函数并从调用代码中导入它。
SWIG:为 C/C++ 库生成包装器代码。您最终应该得到一个可以导入和使用的 python 模块。
Boost.Python:这是我了解最少的一个。在我看来,它与 SWIG 类似,尽管您自己编写了包装器层,但得到了 Boost 宏/函数的大量帮助。
Psyco:稍微加快你的Python代码速度,我在这方面从来没有什么运气。我不会浪费你的时间。分析您的代码,找到瓶颈并使用上述技术之一加速它们。
I haven't used all these methods although I have investigated them all at one point or another...
The Python C API: For writing C code that compiles to a python module that can be imported in Python. Or for writing a Python module that acts as "glue" code to interface with some C library.
scipy.weave: Allows you to shove bits of C code into your python code, if you're using NumPy and SciPy for doing numeric work, look into this. The C code would be as a string, like, weave.inline('printf("%s", foo)') for example.
ctypes: A python module that allows you to call in to C code from your python code. You basically import the shared library then make calls into its API. Some work needed to marshall data in and out of those calls. If you're looking at using an existing C library that you or someone else wrote, I'd start here.
pyrex/cython: Allows you to write Python code (using some special syntax) that will get generated into C code (which can be imported as a Python module) and, obviously, run faster than if it was run through the Python interpreter. This is kind of like the "Python C API" route, only it generates the C code for you. Useful if you have some chunk of code that is your bottleneck and is really slow. Rewrite that function using cython and import it from the calling code.
SWIG: Generates wrapper code for a C/C++ library. You should end up with a python module you can import and use.
Boost.Python: This is the one I know the least about. Looks to me like it's similar to SWIG although you write the wrapper layer yourself, but with a lot of help from Boost macros/functions.
Psyco: Speeds up your python code a bit, I've never had much luck with this. I wouldn't waste your time with it. Profile your code, find your bottlenecks and speed them up using one of the above techniques.
这只是对您问题的一部分的简短回答,但是:
当您有一个想要与 Python 一起使用的预先存在的 C 库时,
ctypes
可能是最好的选择。当您想用 C 编写一些利用 Python 各方面的内容,或者想用 C 编写 Python 扩展时,Python C API 是最好的选择。(Cython 是实现此目的的另一种方法。)
当然,这两种方法都可能在您问题中链接到的SO问题的一些答案中更详细地阐述了。
This is only a brief answer to a portion of your question, but:
ctypes
is probably best when you have a preexisting C library that you want to use with Python.The Python C API is best when you either want to write something in C that utilizes aspects of Python, or want to write an extension for Python in C. (Cython is another way of doing this.)
Of course, both of those are likely elaborated on in much more detail in some of the answers to the SO questions you link to in your question.