cpython教程_python高性能扩展工具-cython教程1快速入门
Cython不仅仅是一种编程语言。它的起源可以追溯到SAGE数学软件包,它用于提高数学计算性能,例如涉及矩阵的计算。更一般地说,我倾向于将Cython视为SWIG的替代品,为本机代码生成非常好的Python绑定。
SWIG是最早和最好之一,用于生成多种语言的绑定的工具。 Cython仅限Python代码。
通过生成语言绑定来处理遗留软件的很好方式,对C / C ++编写的遗留应用程序,用Python添加新功能。
第一章将专注于使用Cython的核心概念
安装Cython
Hello World
使用distutils
Python调用C函数
类型转换
安装
Linux及Mac:
pip install Cython
Linux发行版本:
$ yum install cython
# will work on Fedora and Centos
$ apt-get install cython # will work on Debian based systems.
Hello World!
helloworld.pyx
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: xurongzhong#126.com
# CreateDate: 2018-9-20
# 技术支持qq群: 144081101 591302926 567351477 钉钉免费群:21745728
print("Hello World from cython!")
Makefile
all:
cython -3 -o helloworld.c helloworld.pyx
gcc -g -O2 -fpic -c helloworld.c -o helloworld.o `python3-config --cflags`
gcc -g -O2 -shared -o helloworld.so helloworld.o `python3-config --libs`
clean:
rm -rf *.c *.o *.so build
执行
$ make
cython -3 -o helloworld.c helloworld.pyx
gcc -g -O2 -fpic -c helloworld.c -o helloworld.o `python3-config --cflags`
gcc -g -O2 -shared -o helloworld.so helloworld.o `python3-config --libs`
$ python
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import helloworld
Hello World from cython!
image.png
使用distutils编译
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: xurongzhong#126.com
# CreateDate: 2018-9-20
# 技术支持qq群: 144081101 591302926 567351477 钉钉免费群:21745728
from distutils.core import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize("helloworld.pyx")
)
执行
$ python setup.py build_ext --inplace
running build_ext
building 'helloworld' extension
gcc -pthread -B /usr/local/anaconda/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/anaconda/include/python3.6m -c helloworld.c -o build/temp.linux-x86_64-3.6/helloworld.o
gcc -pthread -shared -B /usr/local/anaconda/compiler_compat -L/usr/local/anaconda/lib -Wl,-rpath=/usr/local/anaconda/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-3.6/helloworld.o -o /home/andrew/code/cython-book/chapter1/helloworld/helloworld.cpython-36m-x86_64-linux-gnu.so
$ python
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import helloworld
Hello World from cython!
此处如果不添加 --inplace,则编译在默认目录
$ python setup.py build_ext
running build_ext
building 'helloworld' extension
gcc -pthread -B /usr/local/anaconda/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/anaconda/include/python3.6m -c helloworld.c -o build/temp.linux-x86_64-3.6/helloworld.o
gcc -pthread -shared -B /usr/local/anaconda/compiler_compat -L/usr/local/anaconda/lib -Wl,-rpath=/usr/local/anaconda/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-3.6/helloworld.o -o build/lib.linux-x86_64-3.6/helloworld.cpython-36m-x86_64-linux-gnu.so
在root下面执行python3 setup.py install则会安装为系统库
# python3 setup.py build_ext
running build_ext
# python3 setup.py install
running install
running build
running build_ext
running install_lib
copying build/lib.linux-x86_64-3.6/helloworld.cpython-36m-x86_64-linux-gnu.so -> /usr/local/anaconda/lib/python3.6/site-packages
running install_egg_info
Writing /usr/local/anaconda/lib/python3.6/site-packages/UNKNOWN-0.0.0-py3.6.egg-info
Python调用C函数
AddFunction.c
#include
int AddFunction(int x, int y) {
printf("look we are within your c code!!\n");
return x + y;
}
AddFunction.h
#ifndef __ADDFUNCTION_H__
#define __ADDFUNCTION_H__
extern int AddFunction(int, int);
#endif //__ADDFUNCTION_H__
PyAddFunction.pyx
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: xurongzhong#126.com
# CreateDate: 2018-9-20
# 技术支持qq群: 144081101 591302926 567351477 钉钉免费群:21745728
cdef extern from "AddFunction.h":
cdef int AddFunction(int, int)
def Add(a, b):
return AddFunction(a, b)
执行
$ make
cython -3 PyAddFunction.pyx
gcc -g -O2 -fpic -c PyAddFunction.c -o PyAddFunction.o `python3-config --includes`
gcc -g -O2 -fpic -c AddFunction.c -o AddFunction.o
gcc -g -O2 -shared -o PyAddFunction.so AddFunction.o PyAddFunction.o `python3-config --libs`
$ python
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from PyAddFunction import Add
>>> Add(1,2)
look we are within your c code!!
3
参考资料
cpython教程_python高性能扩展工具-cython教程1快速入门相关推荐
- Python学习教程(Python学习路线_Python基础学习教程_Python视频教程):初学者新手怎样快速入门Python
Python学习教程(Python学习路线_Python基础学习教程_Python视频教程):初学者新手怎样快速入门Python? 人生苦短,我用Python!!!短短几个字,现在在各大学习类平台随处 ...
- python插件使用教程_Python常用扩展插件使用教程解析
扩展插件 我下载的本版自带 pip下载工具 cmd-pip 下载插件 pip install HTMLParser 如果提示版本问题,更新PIP 别用开始里面的CMD 使用管理者权限 请注意差别 输入 ...
- python自动化教程_Python 任务自动化工具 tox 教程
1.tox 能做什么? 细分的用途包括: 创建开发环境 运行静态代码分析与测试工具 自动化构建包 针对 tox 构建的软件包运行测试 检查软件包是否能在不同的 Python 版本/解释器中顺利安装 统 ...
- python调用c++_python高性能编程之Cython篇 第一章
第一节 cython的潜能 •Cython是一种编程语言,它将Python与C和C ++的静态类型系统相结合. •Cython是一个将Cython源代码转换为高效的C或C ++源代码的编译器.然后可以 ...
- python图形化界面教程_python图形化界面开发教程
python图形化界面开发教程内容摘要 python图形化界面开发教程白萝卜:泰兴电工教程,白了点,白兰地是在红葡萄酒的基础.基金从业资格教程学校,白开水.苜蓿干草.提摩西干草.兔粮方法:白居易< ...
- python入门教程软件-程序员带你十天快速入门Python,玩转电脑软件开发(四)
本系列文章立志于从一个已经习得一门编程语言的基础之上,全面介绍Python的相关开发过程和相关经验总结.本篇文章主要是基于上一篇的程序员带你十天快速入门Python,玩转电脑软件开发(三)的基础之上, ...
- 有没有matlab软件教程基础讲课的老师,详解MATLAB快速入门与应用之MATLAB软件教学视频...
本教程为<详解MATLAB快速入门与应用>的DVD部分,没有PDF教材, 本文件夹中提供各章范例的源程序.程序编号与书中例子的编号不完全相同,如"2.1.1-5"表示为 ...
- 高性能web平台【Lua语言快速入门】
Lua快速入门 一.Lua概述 1.1 Lua是什么 Lua 是一个小巧精妙的脚本语言,诞生于巴西的大学实验室,这个名字在葡萄牙语里的含义是"美丽的月亮".Lua开发小组的目标是开 ...
- python3-pwntools教程_python的pwntools工具的日常使用
1.安装 操作系统: ubuntu16.04 环境准备: python pip libssl-dev libffi-dev pwntools安装: sudo apt-get install libff ...
最新文章
- Git学习笔记;Git bash 库同步问题
- C#设计模式系列 8 ----Builder 生成器模式之--发工资了,带老婆到 岗顶百脑汇配置电脑...
- 五一节快乐~ 顺便写给博友【小诺的网络技术课堂】 小盆友~
- marven编译时:<pre>错误: 不允许使用自关闭元素</pre>
- 关于安卓手机无法将外置声卡的效果录入到手机自拍视频上的问题。
- 前端 flex: 1; 到底是什么意思?
- 如何让一个已经存在的项目跑起来
- Oracle中的序列,同义词
- 免费把pdf转换成excel
- eclipse安装hadoop插件教程
- Deferred Shading VS Deferred Lighting
- java 环境变量的设置
- 数据结构 实验14(1-2班):(深入理解索引存储结构)三元组存储的稀疏矩阵建立行列索引并求鞍点
- ESP8266 下载报错please check partition type 6 addr:3fd000 len:3000
- vuex module总结
- Linux shell test命令用法详解
- 前端-进击的巨人-青铜篇
- 通用票据识别/通用文字识别/通用表格识别/手写识别简述
- 魔兽争霸3地图(WarIII Maps):神魔之井
- 企业电子商务网站策划分析
热门文章
- linux传输tcp命令,Linux tcpdump命令帮助和示例
- vue 组件属性监听_详解vuex 中的 state 在组件中如何监听
- python自动关闭弹窗_python – 每隔x分钟创建一个tkinter窗口,然后在y秒后自动关闭它...
- js传中文参数 java取_js中文转码传输java后台 适用于用url传递中文参数
- Windows 环境 Jenkins集成构建SonarQube
- 用 Excel+VBA 与 SQL Server 数据库交互
- centos安装rabbitmq_【SpringBoot MQ系列教程】RabbitMq 初体验
- 努比亚手机浏览器 安全证书失效_浏览器提示“该站点安全证书的吊销信息不可用”的解决方法-...
- php读取模板生成静态功能,php 生成静态页面的办法与实现代码详细版
- qt使用样式表设置窗口widget为圆角