参考:http://docs.cython.org/en/latest/
变量
python |
C/C++ |
cython |
a=True |
bool a=1/True |
cdef/cpdef bint a=1 |
a=”2” |
char* a=’2’ |
cdef/cpdef char* a=’2’ |
a=”2123bc” |
char* a=”2123bc” |
cdef/cpdef char* a=”2123bc” |
a=2 |
int a=2 |
cdef/cpdef int a=2 |
a=2 |
short int a=2 |
cdef/cpdef short int a=2 |
a=2.0 |
double a=2.0 |
cdef/cpdef double a=2.0 |
a=2.0 |
float a=2.0 |
cdef/cpdef float a=2.0 |
a=np.zeros(10) |
double[] a=np.zeros(10) |
cdef/cpdef double[:] a=np.zeros(10) |
a=np.zeros(10,10) |
double[,] a=np.zeros(10,10) |
cdef/cpdef double[:,:] a=np.zeros(10,10) |
cdef int x,y,z
cdef char *s
cdef float x = 5.2 (single precision)
cdef double x = 40.5 (double precision)
cdef list languages
cdef dict abc_dict
cdef tuple lan
cdef object thing
模块导入
python |
C/C++ |
cython |
import |
_ |
cimport |
from math/cmath import exp |
include math |
from libc.math cimport exp |
类/函数
python |
C/C++ |
cython |
class |
class |
cdef/cpdef cppclass |
def rbf_network(X, beta, theta) |
double[:] rbf_network(double[:, :] X, double[:] beta, double theta) |
cdef/cpdef/def double[:] rbf_network(double[:, :] X, double[:] beta, double theta) |
def, cdef, and cpdef
def
- 常规的python函数,仅来自Python的调用。
cdef
- cython只有函数,不能从python-only代码访问这些函数,必须在Cython中进行访问,因为对于这些函数将不会有C语言转换。
cpdef
- C和Python。 将为Python创建一个C函数和一个包装器。 为什么不总是使用cpdef? 在某些情况下,你可能只有C指针,就像C数组一样。 但是,我们将主要使用cpdef。
调用C函数
参考:http://cython.readthedocs.io/en/latest/src/tutorial/external.html
In [23]: %%cython
...: from libc.stdlib cimport atoi
...: from libc.string cimport strlen
...: cdef char* s="1234567"
...: print(strlen(s))
...: print(atoi(s))
...:
7
1234567
In [24]: %%cython
...: from cpython.set cimport PyAnySet_Check
...: a={1,2,3,4}
...: print(PyAnySet_Check(a))
...:
True
In [25]: %%cython
...: from cpython.set cimport PySet_Add
...: a={1,2,3,4}
...: print(PySet_Add(a,5))
...: print(a)
...:
0
{1, 2, 3, 4, 5}
In [28]: %%cython
...: from cpython.set cimport PySet_Pop
...: a={1,2,3,4,5}
...: print(PySet_Pop(a))
...: print(a)
...:
1
{2, 3, 4, 5}
In [29]: %%cython
...: from numpy.math cimport PI
...: print(PI)
...:
3.141592653589793
In [30]: %%cython
...: from numpy.math cimport E
...: print(E)
...:
2.718281828459045
In [32]: %%cython
...: from libc.math cimport sin
...: cdef int a=30
...: print(sin(a/180))
...:
0.16589613269341502
In [33]: %%cython
...: cdef extern from "math.h":
...: double sin(double x)
...: print(sin(30/180))
...:
0.16589613269341502
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
ext_modules=[
Extension("demo",
sources=["demo.pyx"],
libraries=["m"]
)
]
setup(
name = "Demos",
ext_modules = cythonize(ext_modules)
)
In [42]: %%cython
...: from libc.string cimport strstr
...: cdef char* data = "hfvcakdfagbcffvschvxcdfgccbcfhvgcsnfxjh"
...: pos = strstr(needle='akd', haystack=data)
...: print (pos)
...:
b'akdfagbcffvschvxcdfgccbcfhvgcsnfxjh'
In [3]: %%cython
...: cdef extern from "string.h":
...: char* strstr(const char *haystack, const char *needle)
...: cdef char* data = "hfvcakdfagbcffvschvxcdfgccbcfhvgcsnfxjh"
...:
...: pos = strstr(needle='akd', haystack=data)
...: print(pos)
...:
...:
b'akdfagbcffvschvxcdfgccbcfhvgcsnfxjh'