『Python CoolBook』Cython_高效数组操作
数组运算加速是至关科学计算重要的领域,本节我们以一个简单函数为例,使用C语言为python数组加速。
一、Cython
本函数为一维数组修剪最大最小值
version1
@cython.boundscheck(False)
@cython.wraparound(False)
cpdef clip(double[:] a, double min, double max, double[:] out):
'''
Clip the values in a to be between min and max. Result in out
'''
if min > max:
raise ValueError("min must be <= max")
if a.shape[0] != out.shape[0]:
raise ValueError("input and output arrays must be the same size")
for i in range(a.shape[0]):
if a[i] < min:
out[i] = min
elif a[i] > max:
out[i] = max
else:
out[i] = a[i]
利用Cython类型的内存视图,极大的简化了数组的操作。
cpdef clip()声明了clip()同时为C级别函数以及Python级别函数。 在Cython中,这个是很重要的,因为它表示此函数调用要比其他Cython函数更加高效 (比如你想在另外一个不同的Cython函数中调用clip())。- 类型参数
double[:] a和double[:] out声明这些参数为一维的双精度数组。 作为输入,它们会访问任何实现了内存视图接口的数组对象,这个在PEP 3118有详细定义。 包括了NumPy中的数组和内置的array库。 clip()定义之前的两个装饰器可以优化下性能:@cython.boundscheck(False)省去了所有的数组越界检查, 当你知道下标访问不会越界的时候可以使用它@cython.wraparound(False)消除了相对数组尾部的负数下标的处理(类似Python列表)
version2_条件表达式
任何时候处理数组时,研究并改善底层算法同样可以极大的提示性能
@cython.boundscheck(False)
@cython.wraparound(False)
cpdef clip(double[:] a, double min, double max, double[:] out):
if min > max:
raise ValueError("min must be <= max")
if a.shape[0] != out.shape[0]:
raise ValueError("input and output arrays must be the same size")
for i in range(a.shape[0]):
out[i] = (a[i] if a[i] < max else max) if a[i] > min else min
version3_释放GIL
释放GIL,这样多个线程能并行运行,要这样做的话,需要修改代码,使用 with nogil:
@cython.boundscheck(False)
@cython.wraparound(False)
cpdef clip(double[:] a, double min, double max, double[:] out):
if min > max:
raise ValueError("min must be <= max")
if a.shape[0] != out.shape[0]:
raise ValueError("input and output arrays must be the same size")
with nogil:
for i in range(a.shape[0]):
out[i] = (a[i] if a[i] < max else max) if a[i] > min else min
编写setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext ext_modules = [
Extension('sample',
['sample.pyx'])
] setup(
name = 'Sample app',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)
使用 python3 setup.py build_ext --inplace 来构建它
效率测试示意如下,
>>> import sample
>>> import numpy
>>> b = numpy.random.uniform(-10,10,size=1000000)
>>> c = numpy.zeros_like(b)
>>> import timeit
>>> timeit.timeit('numpy.clip(b,-5,5,c)','from __main__ import b,c,numpy',number=1000)
>>> timeit.timeit('sample.clip(b,-5,5,c)','from __main__ import b,c,sample',
... number=1000)
其中使用numpy自己的clip对比试验,
2.6287411409430206 # numpy
2.8034782900940627 # v1
2.7247575907967985 # v2
2.6071253868285567 # v3
版本三近似于numpy的实现效果,其他版本差一些(每次试验结果都会略有差异,这里只是粗略的比较一下)。
二维数组处理版本参考:
@cython.boundscheck(False)
@cython.wraparound(False)
cpdef clip2d(double[:,:] a, double min, double max, double[:,:] out):
if min > max:
raise ValueError("min must be <= max")
for n in range(a.ndim):
if a.shape[n] != out.shape[n]:
raise TypeError("a and out have different shapes")
for i in range(a.shape[0]):
for j in range(a.shape[1]):
if a[i,j] < min:
out[i,j] = min
elif a[i,j] > max:
out[i,j] = max
else:
out[i,j] = a[i,j]
二、自己写接口
sample.c中添加
/* n:longth of array */
void clip(double *a, int n, double min, double max, double *out) {
double x;
for (; n >= 0; n--, a++, out++) {
x = *a; *out = x > max ? max : (x < min ? min : x);
}
}
pysample.c中添加
// void clip(double *a, int n, double min, double max, double *out);
static PyObject *py_clip(PyObject *self, PyObject *args){
PyObject *a, *out;
int min, max;
if(!PyArg_ParseTuple(args, "OiiO", &a, &min, &max, &out)){ //py数组对象暂记
return NULL;
} // printf("%i, %i\n", min, max);
Py_buffer view_a, view_out; //py数组对象接收对象
if (PyObject_GetBuffer(a, &view_a,
PyBUF_ANY_CONTIGUOUS | PyBUF_FORMAT) == -1) {
return NULL;
}
if (PyObject_GetBuffer(out, &view_out,
PyBUF_ANY_CONTIGUOUS | PyBUF_FORMAT) == -1) {
return NULL;
}
clip(view_a.buf, view_a.shape[0], min, max, view_out.buf);
PyBuffer_Release(&view_a);
PyBuffer_Release(&view_out);
return Py_BuildValue("");
}
函数登记处添加
{"clip", py_clip, METH_VARARGS, "clip array"},
则可,实际测试发现,手动接口效率也很高,和numpy同一水平。
『Python CoolBook』Cython_高效数组操作的更多相关文章
- 『Python CoolBook』Cython
github地址 使用Cython导入库的话,需要一下几个文件: .c:C函数源码 .h:C函数头 .pxd:Cython函数头 .pyx:包装函数 setup.py:python 本节示例.c和.h ...
- 『Python CoolBook』使用ctypes访问C代码_下_demo进阶
点击进入项目 这一次我们尝试一下略微复杂的c程序. 一.C程序 头文件: #ifndef __SAMPLE_H__ #define __SAMPLE_H__ #include <math.h&g ...
- 『Python CoolBook』C扩展库_其三_简单数组操作
点击进入项目 这里的数组要点在于: 数组结构,array.array或者numpy.array 本篇的数组仅限一维,不过基础的C数组也是一维 一.分块讲解 源函数 /* Average values ...
- 『Python CoolBook』C扩展库_其四_结构体操作与Capsule
点击进入项目 一.Python生成C语言结构体 C语言中的结构体传给Python时会被封装为胶囊(Capsule), 我们想要一个如下结构体进行运算,则需要Python传入x.y两个浮点数, type ...
- 『Python CoolBook』C扩展库_其六_线程
GIL操作 想让C扩展代码和Python解释器中的其他进程一起正确的执行, 那么你就需要去释放并重新获取全局解释器锁(GIL). 在Python接口封装中去释放并重新获取全局解释器锁(GIL),此时本 ...
- 『Python CoolBook』C扩展库_其五_C语言层面Python库之间调用API
点击进入项目 一.C层面模块添加API 我们仍然操作如下结构体, #include <math.h> typedef struct Point { double x,y; } Point; ...
- 『Python CoolBook』C扩展库_其六_从C语言中调用Python代码
点击进入项目 一.C语言运行pyfun的PyObject对象 思路是在C语言中提供实参,传给python函数: 获取py函数对象(PyObject),函数参数(C类型) 获取GIL(PyGILStat ...
- 『Python CoolBook』数据结构和算法_字典比较&字典和集合
一.字典元素排序 dict.keys(),dict.values(),dict.items() 结合max.min.sorted.zip进行排序是个很好的办法,另外注意不使用zip时,字典的lambd ...
- 『Python CoolBook』使用ctypes访问C代码_上_用法讲解
一.动态库文件生成 源文件hello.c #include "hello.h" #include <stdio.h> void hello(const char *na ...
随机推荐
- ts-loader 安装问题
首先,有个问题:ts-loader是将typescript转成javascript,转成哪个版本的javascript版本? 查询到参考地址:http://morning.work/page/othe ...
- 理解Deadlock
问:为啥以下代码会产生死锁 public class Deadlock { static class Friend { private final String name; public Friend ...
- 取数游戏II
传送门 #include <bits/stdc++.h> using namespace std; #define ll long long #define re register #de ...
- Deeplab v3+的结构的理解,图像分割最新成果
Deeplab v3+ 结构的精髓: 1.继续使用ASPP结构, SPP 利用对多种比例(rates)和多种有效感受野的不同分辨率特征处理,来挖掘多尺度的上下文内容信息. 解编码结构逐步重构空间信息来 ...
- 关于UI适配的文档
第一部分:原理 (1)根据当前屏幕尺寸与开发预设屏幕尺寸尺寸得出以下参数. 1 XRatio:当前屏幕尺寸与开发尺寸的X轴比例 2 YRtaio:当前屏幕尺寸与开发尺寸的Y轴比例 3minRatio: ...
- Exercise about Shape
#include <iostream> using namespace std; class point { int x; int y; public : point () { x=y=; ...
- CentOS 7 之 Systemd 入门教程:实战篇
开机启动对于那些支持 Systemd 的软件,安装的时候,会自动在/usr/lib/systemd/system目录添加一个配置文件. 如果你想让该软件开机启动,就执行下面的命令(以httpd.ser ...
- 游戏客户端Session的统一管理
看本系统文章需要些C语言.数据结构和网络基础知识! 说明:由于游戏服务器端会有成千上万的客户端进行连接请求,所以要求我们需要做一些简单的会话管理!如下图 1.简单说明 进行统一的分配和管理,就需要我们 ...
- scrapy item pipeline
item pipeline process_item(self, item, spider) #这个是所有pipeline都必须要有的方法在这个方法下再继续编辑具体怎么处理 另可以添加别的方法 ope ...
- C# 使用lambda表达式过滤掉数组中的空字符串
使用lambda表达式过滤掉数组中的空字符串 KeyWord = KeyWord.Where(S => !string.IsNullOrEmpty(S)).ToArray();