Pytorch 学习(10):Python Cython扩展(python pyx代码-----C 代码 ----python代码调用)

 Cython是具有C数据类型的Python,几乎任何一个Python代码都是有效的Cython代码。Cython编译器将把python代码转换成C代码,对Python/C API进行等效调用。

 python代码------>C 代码 ------->python代码调用

 在spyder中新建一个fib.pyx文件:

def fib(long n):
    '''Returns the nth Fibonacci number.'''
    cdef long a=0, b=1, i
    for i in range(n):
        a, b = a + b, a
    return a

 

spyder新建一个setup.py文件:

# -*- coding: utf-8 -*-

from distutils.core import setup
from Cython.Build import cythonize
#cythonize:编译源代码为C或C++,返回一个distutils Extension对象列表
setup(ext_modules=cythonize('fib.pyx'))
 

进入setup的当前目录,在windows cmd命令行中运行:

D:\vcProjects2018\clion\cython_exercise>python setup.py build_ext -i
Compiling fib.pyx because it changed.
[1/1] Cythonizing fib.pyx
running build_ext
building 'fib' extension
creating build
creating build\temp.win-amd64-3.6
creating build\temp.win-amd64-3.6\Release
G:\Program Files (x86)\Microsof

你可能感兴趣的:(AI,&,Big,Data案例实战课程)