Cython(一)
来源:互联网 发布:网络电视剧排名榜 编辑:程序博客网 时间:2024/05/01 13:52
1、 Ubuntu下安装
apt-get install cython
2 第一个例子:
2.1 创建helloworld目录
创建helloworld.pyx,内容如下:
cdef extern from"stdio.h": extern int printf(const char *format, ...) def SayHello(): printf("hello,world\n")
代码非常简单,就是调用了C函数printf打印hello,world
3 如何编译
3.1 最方便的当然是利用python的Distutils了,看下如何来实现
先在helloworld目录下创建Setup.py,内容如下:
from distutils.core import setupfrom distutils.extension import Extensionfrom Cython.Build import cythonizesetup( name = 'helloworld', ext_modules=cythonize([ Extension("helloworld", ["helloworld.pyx"]), ]),)
编译:
python Setup.py build
安装:
python Setup.py install
安装后,会将在build/lib.???目录下生成的helloworld.pyd拷贝到Lib/site-packages
注:
有时我们只是希望测试一下,并不希望安装,这时可以把build/lib.???目录下的helloworld.pyd拷贝到当前目录
或者在importhelloworld前执行脚本:import sys;sys.path.append(pathof helloworld.pyd)
测试:
import helloworld helloworld.SayHello() hello,world
3.2 其次就是可以自己写Makefile进行编译
写Makefile的好处就是可以知道编译的实质:
下面是用于Windows下编译的Makefile,Makefile内容如下:
ALL :helloworld.pydhelloworld.c : helloworld.pyx cython -o helloworld.c helloworld.pyxhelloworld.obj :helloworld.c cl -c -Id:\python27\include helloworld.c helloworld.pyd :helloworld.obj link /DLL /LIBPATH:d:\python27\libshelloworld.obj /OUT:helloworld.pyd
执行命令:
set PATH=D:\Python27\Scripts;%PATH%make
0 0
- Cython(一)
- Cython基础--Cython入门
- Cython基础--Cython入门
- Cython基础--Cython入门
- Cython基础--Cython入门
- 【Cython】Cython 基本用法
- cython 试用
- Cython红黑树版
- Cython 101
- Cython初窥
- IPython & Cython
- Cython环境.
- 什么是Cython
- Cython入门
- Cython初探
- Cython安装
- cython初探
- Cython学习
- RecycleView的用法
- node to traverse cannot be null!
- 跟我学Redis 目录
- 剑指offer--面试题64:数据流中的中位数
- 深度学习环境搭建 ubuntu16+GTX1080+CUDA8+Theano+Tensorflow+keras
- Cython(一)
- 百度,高德,天地图Android导航模块对比
- PAT(A) - 1075. PAT Judge (25)
- 【MYSQL】查询不区分大小写
- 微服务之RPC
- 系统设计过程
- 剑指offer面试题28: 字符串的排列
- 38. Count and Say
- Ajax同步与异步优缺点