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!

47c42ae044a0

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

参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值