Skip navigation.

生活周遭

In My Circle

Pyrex, 和Python的C扩展

,

这原本是回复到[python-chinese]邮件列表的,但是没有回复进去,可能是因为功过Gmane news服务器的原因。先转到这儿:

倒是这两天我也在看这方面的问题~
一般编写Python C/C++ Module,或者为C/C++ lib编写Wrapper,我所看到的有以下几种方式: 1. 直接使用C Python提供的API 2. SWIG 3. Boost.Python 4. Pyrex

首先直接使用Python C API是很不方便的。上面几位高人已经谈过了SWIG和Boost.Python,我就不多说了,只是个人感觉易用性而言Boost.Python > SWIG,但SWIG功能强大,支持的也不只是Python一种脚本语言。以上这两种方式对C++的支持都比较友好。

这里着重推荐一下Pyrex。 Pyrex可以说是扩展Python的一大创新,她使用Python like的语法来编写Python的C Module,自动翻译成C语言代码,进而编译获取C代码的高效率。而且,配合Python的Distutils,使得构建过程简单到了只需要setup.py的程度。我觉得其简化python扩展的编写方式,已经和Boost.python,SWIG不在一个意义级别上了。

这里贴一段来自pyrex的示例,用于搜索质数:

# primes.pyx
#
#  Calculate prime numbers
#
 
def primes(int kmax):
        cdef int n, k, i
        cdef int p[1000]
        result = []
        if kmax > 1000:
                kmax = 1000
        k = 0
        n = 2
        while k < kmax:
                i = 0
                while i < k and n % p <> 0:
                        i = i + 1
                if i == k:
                        p[k] = n
                        k = k + 1
                        result.append(n)
                n = n + 1
        return result

将其pyrexc编译后得到一个c文件,然后编译之后就是一个python module了~ 按照他的逻辑,我还对照相应的写了一个Python脚本:

#!/usr/bin/env python
# primespy.py
 
def primes(kmax):
        result = []
        if kmax > 1000:
                kmax = 1000
        k = 0
        n = 2
        while k < kmax:
                i = 0
                while i< k and n % result != 0:
                        i += 1
                if i == k:
                        result.append(n)
                        k += 1
                n+=1
        return result
两者的代码行数基本一样,来看一下运行结果:
In [23]: tpyx = timeit.Timer(stmt='primes.primes(1000)', setup='import primes')
 
In [24]: tpy = timeit.Timer(stmt='primespy.primes(1000)', setup='import primespy')
 
In [25]: tpyx.timeit(100)
Out[25]: 1.2969999313354492
 
In [26]: tpy.timeit(100)
Out[26]: 30.266000032424927
速度提高了30倍之多!

pyrex的优势是编写简单,不必处理多余的细节,而且也不需要为这种简单付出效率的代价。劣势在于目前的pyrex对C++的只是仍然不是很好,还在继续开发之中。至于对C++到地支持差到什么程度,我还没有进一步测试过,还希望看到各位的高见。

总结是,如果是针对C的Python扩展,或者lib Wrapper,Pyrex可以说是最方便的选择。如果涉及到C++的扩展,可 能目前boost.python还是最好的选择了。

补:目前正在看Boost.python,SWIG其实并没有细看过,呵呵,只是看了一些intro性质文章,就开始大发厥词了,希望大家补充指正。

fglrx ATI Linux driver进入Debian了Vim7的新功能

Comments

Anonymous 11. June 2006, 09:12

Anonymous writes:

如果利用pyrex为python编写关于C++的扩展呢?

Leira Hua 13. June 2006, 04:54

应朋友的要求把Anonymous Comments打开了,不过,楼上的仁兄,至少请把名字填上啊~

官方的说法是这样的:
<quote>
C++ Compilable
The generated code should now be compilable as either C or C++ without errors (although there may still be warnings). However, note that you can still only call C++ functions if they have been declared "extern C", even if you compile the Pyrex output as C++. I hope to introduce some C++ interface features soon.
<quote>

而且好像还有一个用于改善C++支持的patch,不过似乎也是有限。呵呵,我没有亲身试过,不敢瞎说~

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies

January 2010
S M T W T F S
December 2009February 2010
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30