cython安装、使用

一、cython 在linux(ubuntu)下安装


sudo apt-get install cython

安装后  输入 cython 即可验证是否安装成功


二、 使用

1、编写   以 .pyx为扩展名的 cython程序,hello.pyx

[python] view plain copy
  1. def say_hello_to(name):  
  2.     print("Hello %s!" % name)  

 2、编写python程序 setup.py,其目的是把 hello.pyx程序转化成hello.c ,并编译成so文件
[python] view plain copy
  1. from distutils.core import setup  
  2. from distutils.extension import Extension  
  3. from Cython.Distutils import build_ext  
  4.   
  5. ext_modules = [Extension("hello", ["hello.pyx"])]  
  6.   
  7. setup(  
  8.   name = 'Hello world app',  
  9.   cmdclass = {'build_ext': build_ext},  
  10.   ext_modules = ext_modules  
  11. )  

3. 执行python程序
[python] view plain copy
  1. zero@zero:~$ python setup.py build_ext --inplace  

执行的结果会生成两个文件:hello.c 和 hello.so( 用PyObject* 封装好的文件)

4. 用python调用 hello.so,调用文件为test.py

[python] view plain copy
  1. import hello  
  2.   
  3. hello.say_hello_to("hi,cython!!")  


cython的主要目的是: 简化python调用c语言程序的繁琐封装过程,提高python代码执行速度(C语言的执行速度比python快)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值