python与c混编生成模块-Cython

Cython

用来快速生成Python扩展模块的工具;
Python的编译器,用于提高python速度,通过OpenMPI库还可以进行吧并行计算;
它的语法是c和python的混血;

例子

创建hello.pyx,内容如下

def say_hello():
    print "Hello World!"

创建setup.py,内容如下

from distutils.core import setup
from Cython.Build import cythonize
setup(name = 'Hello world app',
      ext_modules = cythonize("hello.pyx"))

编译Cython代码
step1: 把.pyx文件被Cython编译为.c文件
step2: 把.c文件编译为可导入的使用模块.so(Windows下为.pyd)文件

python setup.py build
python setup.py install

这时候会报错,如果安装vs2010需要在cmd中安装

SET VS90COMNTOOLS=%VS100COMNTOOLS%

然后执行

>>> import hello
>>> hello.say_hello()
Hello World!

通过静态类型提高速度

原来的代码如下
compute.pyx

def f(x):
    return x ** 2 - x
def integrate_f(a, b, N):
    s = 0
    dx = (b - a) / N
    for i in range(N):
        x += f(a + i * dx)
    return s * dx

setup.py

from distutils.core import setup
from Cython.Build import cythonize
setup(
  name = 'Hello world app',
  ext_modules = cythonize("compute.pyx"),
)

test.py

import compute
import time
starttime = time.clock()
compute.integrate_f(3.2, 6.9, 1000000)
endtime = time.clock()
print "read: %f s" %(endtime - starttime)

执行及结果

python setup.py build
python setup.py install
python test.py
read: 0.332332 s

静态替换后的代码
compute2.pyx

def f(double x):
    return x ** 2 - x
def integrate_f(double a, double b, int N):
    cdef int i
    cdef double s, dx
    s = 0
    dx = (b - a) / N
    for i in range(N):
        s += f(a + i * dx)
    return s * d

setup2.py

from distutils.core import setup
from Cython.Build import cythonize
setup(
  name = 'Hello world app',
  ext_modules = cythonize("compute2.pyx"),
)

test2.py

import compute2
import time
starttime = time.clock()
compute2.integrate_f(3.2, 6.9, 1000000)
endtime = time.clock()
print "read: %f s" %(endtime - starttime)

执行

python setup.py build
python setup.py install
python test.py
read: 0.109200s

使用静态类型速度是不使用静态类型的3倍

静态类型函数
把compute2.pyx中的函数变为

cdef double f(double x):
    return x ** 2 - x
def integrate_f(double a, double b, int N):
    cdef int i
    cdef double s, dx
    s = 0
    dx = (b - a) / N
    for i in range(N):
        s += f(a + i * dx)
    return s * dx

结果:read: 0.084859 s
比例子3.2.1速度又快了

调用C函数

cdef extern from "math.h":
    double sin(double)
    double cos(double)

cpdef double Sin(double x):
    return sin(x)

cpdef double Cos(double x):
    return cos(x)
  • cpdef: 对于Python可使用的函数使用(为了使得在以后的Python程序中调用Sin,Cos函数,用cpdef,而不用cdef)
  • cdef: 对于C可使用的函数使用

上面的代码声明了math.h里的函数,提供给 Cython 使用。
C编译器在编译时将会看到 math.h 的声明,但 Cython 不会去分析 math.h 和单独的定义。

posted @ 2016-12-23 18:14  zhangshihai1232  阅读(306)  评论(0)    收藏  举报