windows python pyrex 扩展

在偶的ubuntu里编写pyrex程序编译成so还是挺爽的,用 timeit.Timer 测试性能提升不少,今天在windows也尝试了一番。

需要的工具有:
- Pyrex http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/
- Dev C++ http://www.bloodshed.net/devcpp.html

Pyrex 可以通过easy_install Pyrex来安装。
Dev C++ 安装完在系统环境变量Path里加上Dev C++安装目录/bin 目录。

测试扩展代码:

# file: foo.pyx
""" simple pyrex module """
cdef class Foo:
    """ foo doc ... """
    cdef char *name
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return "foo names: %s" % (self.name)

# file: setup.py
from distutils.core import setup
from distutils.extension import Extension
from Pyrex.Distutils import build_ext

setup(
    name='foo', ext_modules=[Extension("foo", ["foo.pyx"])],
    cmdclass={'build_ext':build_ext}
)

写好两个文件后,进入命令提示符:

C:/>python setup.py build_ext --inplace -c mingw32
running build_ext
pyrexc foo.pyx --> foo.c
building 'foo' extension
creating build
creating build/temp.win32-2.5
creating build/temp.win32-2.5/Release
C:/Program Files/DEV-CPP/Bin/gcc.exe -mno-cygwin -mdll -O -Wall -ID:/Python25/include -ID:/Python25/PC -c foo.c -o build/temp.win32-2.5/Release/foo.o writing build/temp.win32-2.5/Release/foo.def C:/Program Files/DEV-CPP/Bin/dllwrap.exe -mno-cygwin -mdll -static --entry _DllMain@12 --output-lib build/temp.win32-2.5/Release/libfoo.a --def build/temp.win32-2.5/Release/foo.def -s build/temp.win32-2.5/Release/foo.o -LD:/Python25/libs -L D:/Python25/PCBuild -lpython25 -lmsvcr71 -o foo.pyd

编译完毕,可以看到当前目录下多了:build目录、foo.c、foo.pyd。foo.pyd 即是编译好的二进制扩展。

C:/>python
ActivePython 2.5.0.0 (ActiveState Software Inc.) based on
Python 2.5 (r25:51908, Mar 9 2007, 17:40:28) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo
>>> foo
<module 'foo' from 'foo.pyd'>
>>> foo.Foo("smallfish")
foo names: smallfish

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值