【Cython】Cython三分钟入门教程

【Cython】Cython三分钟入门教程,第1张

概述<pstyle=\"font-size:14px;font-family:tahoma,arial,\'宋体\';\">我最喜欢的是Python,它的代码优雅而实用,可惜纯粹从速度上来看它比大多数语言都要慢。大多数人也认为的速度和易于使用是两极对立

<p >我最喜欢的是Python,它的代码优雅而实用,可惜纯粹从速度上来看它比大多数语言都要慢。大多数人也认为的速度和易于使用是两极对立的——编写C代码的确非常痛苦。而 Cython 试图消除这种两重性,并让你同时拥有 Python 的语法和 C 数据类型和函数——它们两个都是世界上最好的。请记住,我绝不是我在这方面的专家,这是我的第一次Cython真实体验的笔记:

<p >编辑:根据一些我收到的反馈,大家似乎有点混淆——Cython是用来生成 C 扩展到而不是独立的程序的。所有的加速都是针对一个已经存在的 Python 应用的一个函数进行的。没有使用 C 或 lisp 重写整个应用程序,也没有手写C扩展 。只是用一个简单的方法来整合C的速度和C数据类型到 Python 函数中去。

<p >现在可以说,我们能使下文的 great_circle 函数更快。所谓 great_circle 是计算沿地球表面两点之间的距离的问题:

<p >p1.py

<p >

<code >import math

def great_circle(lon1,lat1,lon2,lat2):
radius = 3956 #miles
x = math.pi/180.0

a = (90.0-lat1)*(x)b = (90.0-lat2)*(x)theta = (lon2-lon1)*(x)c = math.acos((math.cos(a)*math.cos(b)) +              (math.sin(a)*math.sin(b)*math.cos(theta)))return radius*c</code></pre><p https://m.jb51.cc/tag/Font/" target="_blank" >Font</a>-size:14px;<a href="https://m.jb51.cc/tag/Font/" target="_blank" >Font</a>-family:tahoma,'宋体';"&gt;让<a href="https://m.jb51.cc/tag/women/" target="_blank" >我们</a><a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" >调用</a>它 50 万次并测定它的<a href="https://m.jb51.cc/tag/shijian/" target="_blank" >时间</a> :</p><p https://m.jb51.cc/tag/Font/" target="_blank" >Font</a>-size:14px;<a href="https://m.jb51.cc/tag/Font/" target="_blank" >Font</a>-family:tahoma,'宋体';"&gt;</p><pre><code &gt;<a href="https://m.jb51.cc/tag/import/" target="_blank" >import</a> timeit 

lon1,lat2 = -72.345,34.323,-61.823,54.826
num = 500000

t = timeit.Timer("p1.great_circle(%f,%f,%f)" % (lon1,lat2),"import p1")
print "Pure python function",t.timeit(num),"sec"

<p >约2.2秒 。它太慢了!

<p >让我们试着快速地用Cython改写它,然后看看是否有差别:
c1.pyx

<p >

<code >import math

def great_circle(float lon1,float lat1,float lon2,float lat2):
cdef float radius = 3956.0
cdef float pi = 3.14159265
cdef float x = pi/180.0
cdef float a,b,theta,c

a = (90.0-lat1)*(x)b = (90.0-lat2)*(x)theta = (lon2-lon1)*(x)c = math.acos((math.cos(a)*math.cos(b)) + (math.sin(a)*math.sin(b)*math.cos(theta)))return radius*c</code></pre><p https://m.jb51.cc/tag/Font/" target="_blank" >Font</a>-size:14px;<a href="https://m.jb51.cc/tag/Font/" target="_blank" >Font</a>-family:tahoma,'宋体';"&gt;请注意,<a href="https://m.jb51.cc/tag/women/" target="_blank" >我们</a>仍然<a href="https://m.jb51.cc/tag/import/" target="_blank" >import</a> math——cython让您在一定程度上混搭Python和C数据类型在。转换是<a href="https://www.jb51.cc/tag/zidong/" target="_blank" >自动</a>的,但并非没有代价。<a href="https://www.jb51.cc/tag/zaizhe/" target="_blank" >在这</a>个例子中<a href="https://m.jb51.cc/tag/women/" target="_blank" >我们</a>所做的就是定义<a href="https://www.jb51.cc/tag/yige/" target="_blank" >一个</a>Python<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" >函数</a>,声明它的输入<a href="https://m.jb51.cc/tag/canshu/" target="_blank" >参数</a>是浮点数类型,并为所有变量声明类型为C浮点数据类型。计算部分它仍然<a href="https://m.jb51.cc/tag/shiyong/" target="_blank" >使用</a>了Python的 math 模块。</p><p https://m.jb51.cc/tag/Font/" target="_blank" >Font</a>-size:14px;<a href="https://m.jb51.cc/tag/Font/" target="_blank" >Font</a>-family:tahoma,'宋体';"&gt;现在<a href="https://m.jb51.cc/tag/women/" target="_blank" >我们</a>需要将其转换为C<a href="https://www.jb51.cc/tag/daima/" target="_blank" >代码</a>再编译为Python扩展。完成这一部的<a href="https://m.jb51.cc/tag/zuihao/" target="_blank" >最好</a>的办法是编写<a href="https://www.jb51.cc/tag/yige/" target="_blank" >一个</a>名为setup.py<a href="https://m.jb51.cc/tag/fabu/" target="_blank" >发布</a><a href="https://m.jb51.cc/tag/jiaoben/" target="_blank" >脚本</a>。但是,现在<a href="https://m.jb51.cc/tag/women/" target="_blank" >我们</a>用手工方式 ,以了解其中的巫术:</p><p https://m.jb51.cc/tag/Font/" target="_blank" >Font</a>-size:14px;<a href="https://m.jb51.cc/tag/Font/" target="_blank" >Font</a>-family:tahoma,'宋体';"&gt;</p><pre><code &gt;# this will create a c1.c <a href="https://m.jb51.cc/tag/file/" target="_blank" >file</a> - the C source code to build a python extension

cython c1.pyx

Compile the object file

gcc -c -fPIC -I/usr/include/python2.5/ c1.c

link it into a shared library

gcc -shared c1.o -o c1.so


现在你应该有一个c1.so(或.dll)文件,它可以被Python import。现在运行一下:<p >

<code >    t = timeit.Timer("c1.great_circle(%f,"import c1")
print "Cython function (still using python math)","s
<p >约1.8秒 。并没有我们一开始期望的那种大大的性能提升。使用 python 的 match 模块应该是瓶颈。现在让我们使用C标准库替代之:

<p >c2.pyx

<p >

<code >cdef extern from "math.h":
float cosf(float theta)
float sinf(float theta)
float acosf(float theta)

def great_circle(float lon1,c

a = (90.0-lat1)*(x)b = (90.0-lat2)*(x)theta = (lon2-lon1)*(x)c = acosf((cosf(a)*cosf(b)) + (sinf(a)*sinf(b)*cosf(theta)))return radius*cec"

与 import math 相应,我们使用cdef extern 的方式使用从指定头文件声明函数(在此就是使用C标准库的math.h)。我们替代了代价高昂的的Python函数,然后建立新的共享库,并重新测试:
t = timeit.Timer("c2.great_circle(%f,"import c2")
print "Cython function (using trig function from math.h)",'宋体';">现在有点喜欢它了吧?0.4秒 -比纯Python函数有5倍的速度增长。我们还有什么方法可以再提高速度?c2.great_circle()仍是一个Python函数调用,这意味着它产生Python的API的开销(构建参数元组等),如果我们可以写一个纯粹的C函数的话,我们也许能够加快速度。

<p >c3.pyx

<p >

<code >cdef extern from "math.h":
float cosf(float theta)
float sinf(float theta)
float acosf(float theta)

cdef float _great_circle(float lon1,c

a = (90.0-lat1)*(x)b = (90.0-lat2)*(x)theta = (lon2-lon1)*(x)c = acosf((cosf(a)*cosf(b)) + (sinf(a)*sinf(b)*cosf(theta)))return radius*c

def great_circle(float lon1,float lat2,int num):
cdef int i
cdef float x
for i from 0 < = i < num:
x = _great_circle(lon1,lat2)
return x

<p >请注意,我们仍然有一个Python函数( def ),它接受一个额外的参数 num。这个函数里的循环使用for i from 0 < = i < num: ,而不是更Pythonic,但慢得多的for i in range(num):。真正的计算工作是在C函数(cdef)中进行的,它返回float类型。这个版本只要0.2秒——比原先的Python函数速度提高10倍。

<p >为了证明我们所做的已经足够优化,可以用纯C写一个小应用,然后测定时间:

<p >

<code >#include <math .h>

include <stdio .h>define NUM 500000

float great_circle(float lon1,float lat2){
float radius = 3956.0;
float pi = 3.14159265;
float x = pi/180.0;
float a,c;

a = (90.0-lat1)*(x);b = (90.0-lat2)*(x);theta = (lon2-lon1)*(x);c = acos((cos(a)*cos(b)) + (sin(a)*sin(b)*cos(theta)));return radius*c;

}

int main() {
int i;
float x;
for (i=0; i < = NUM; i++)
x = great_circle(-72.345,54.826);
printf("%f",x);
}

<p >用gcc -lm -o ctest ctest.c编译它,测试用time ./ctest ...大约0.2秒 。这使我有信心,我Cython扩展相对于我的C代码也极有效率(这并不是说我的C编程能力很弱)。

<p >能够用 cython 优化多少性能通常取决于有多少循环,数字运算和Python函数调用,这些都会让程序变慢。已经有一些人报告说在某些案例上 100 至 1000 倍的速度提升。至于其他的任务,可能不会那么有用。在疯狂地用 Cython 重写 Python 代码之前,记住这一点:

<p >"我们应该忘记小的效率,过早的优化是一切罪恶的根源,有 97% 的案例如此。"——Donald knuth

<p >换句话说,先用 Python 编写程序,然后看它是否能够满足需要。大多数情况下,它的性能已经足够好了……但有时候真的觉得慢了,那就使用分析器找到瓶颈函数,然后用cython重写,很快就能够得到更高的性能。

<p >外部链接
WorldMill(<a href="http://trac.gispython.org/projects/PCL/wiki/WorldMill" rel="nofollow" >http://trac.gispython.org/projects/PCL/wiki/WorldMill)——由Sean Gillies 用 Cython 编写的一个快速的,提供简洁的 python 接口的模块,封装了用以处理矢量地理空间数据的 libgdal 库。

<p >编写更快的 Pyrex 代码(<a href="http://www.sagemath.org:9001/WritingFastPyrexCode" rel="nofollow" >http://www.sagemath.org:9001/WritingFastPyrexCode)——Pyrex,是 Cython 的前身,它们有类似的目标和语法。

<p >

<p >作者:perrygeo

<p >译者:赖勇浩(http://laiyonghao.com)

<p >原文:http://www.perrygeo.net/wordpress/?p=116

总结

以上是内存溢出为你收集整理的【Cython】Cython三分钟入门教程全部内容,希望文章能够帮你解决【Cython】Cython三分钟入门教程所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1209164.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存