python与cython对应(变量、函数等)

参考:http://docs.cython.org/en/latest/


变量

pythonC/C++cython
a=Truebool a=1/Truecdef/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=2int a=2cdef/cpdef int a=2
a=2short int a=2cdef/cpdef short int a=2
a=2.0double a=2.0cdef/cpdef double a=2.0
a=2.0float a=2.0cdef/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 # list 列表
cdef dict abc_dict # dict 字典
cdef tuple lan # 元组
cdef object thing # 任意对象

模块导入

pythonC/C++cython
import_cimport
from math/cmath import expinclude mathfrom libc.math cimport exp

类/函数

pythonC/C++cython
classclasscdef/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"] # Unix-like specific
    )
]

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'
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python-Cython是一种使Python代码能够编译成C语言扩展的工具库。它包括Cython源码以及Python2.7/Lib/site-package下的cython.py文件。 Cython是基于Pyrex的,但是相比Pyrex,Cython拥有更多的功能和优化。Cython语言与Python语言非常相似,但是还支持调用C函数以及在变量和类属性上声明C类型,这使得Python代码能够以接近原生C语言的速度运行。使用Python-Cython,我们可以将Python代码转化为C语言扩展,从而提高代码的执行效率。使用Python-Cython时,可以在sys.path的头部添加cython_path,以防止Python site-package中的Cython对我们独立出来的Cython模块造成影响。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [python---之cython](https://blog.csdn.net/zxyhhjs2017/article/details/80580585)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [python-cython:https](https://download.csdn.net/download/weixin_42134144/16268191)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值