Pythonç代ç ä¼é
èå®ç¨ï¼ä½æ¯å®çé度确å®ä¸ææ ·ãCythonä¼å¾å¨ä¿çPythonç¾å¥½çåæ¶ï¼å
¼é¡¾ä»£ç çæçï¼è¿éå
è´´åºCythonç两个å®æ¹ææ¡£ï¼ä»¥æ¹ä¾¿å¤§å®¶å¯¹Cythonæ´ç³»ç»çå¦ä¹ ï¼
è±æ http://docs.cython.org/en/latest/
ä¸æ https://moonlet.gitbooks.io/cython-document-zh_cn/content/ch1-basic_tutorial.html
为ä»ä¹æè¿ä¼åCythonæä¸äº¤éï¼è¯´èµ·æ¥æ¯å 为æçäºä¸ç¯åå«
ãCythonä¸åéå
¥é¨ã
çæç« ï¼æ¯«æ çé®æç« åå¾å¾ä¸éï¼ä½æ¯æ é¢èµ·å¾å®å¨ä¸å¥½ï¼å 为太æ é¢å
å¦ï¼
è·éæ´ä¸ªé¡¹ç®æè±äºå¤§æ¦ä¸¤å¤©æ¶é´ï¼å°½ç®¡ä»£ç é并ä¸å¤§ãæ¥ä¸æ¥æä»ç¶ä¼ä»¥æç« ä¸çä¾åæ¥è¿è¡è¯´æï¼ç»åè¿ä¸¤å¤©çç»åï¼ä»ä¸ä¸ªç¯å¢åºåï¼1. Windowsç¯å¢ï¼2. Linuxç¯å¢ï¼3. Cgywinç¯å¢ï¼å°æéå°çé®é¢ä¸å¹¶è®²æ¸
æ¥ï¼å¸æè½å¸®å©å°åææç¸åé®é¢çåå¦ã
Cythonçå®è£
Cythonæ¯ä¸ä¸ªPythonåºï¼çæå½ç¶çå®è£ æ段就æ¯
-> pip intsall cython
å½ç¶å¯¹äºCygwinç¨æ·ï¼ä¹è½å¤è¿å
¥Cygwinå®è£
ç¨åºä¸ä¸è½½Cythonå
ï¼æææ¯ä¸æ ·çï¼æ¥éª¤å¯ä»¥åèæçå¦ä¸ç¯æç« ãCygwinï¼è®©ä½ æ¥æWindowsä¸çLinuxç¯å¢ãã
ç±äºå主æºçé
ç½®ä¸ä¸æ ·ï¼ä½ æµè¯åºæ¥çæ¶é´åæä¸ä¸æ ·å¾æ£å¸¸ï¼ä½æ¯è¿ç§æ§è½çä¸åæ¯å¿
å®åæä¸æ ·çï¼
Pythonçæ¬
ä¸é¢æ¯ä¸æ®µPythonå®ç°ç代ç ï¼ä½ç¨æ¯è®¡ç®æ²¿å°ç表é¢ä¸¤ç¹ä¹é´çè·ç¦»ï¼æ¯æ们çversion1ãæ们ä¼è®©å®è°ç¨50ä¸æ¬¡ï¼å¹¶æµå®å®æéçæ¶é´ï¼
# version1.py
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
å®ç°ä»¥ä¸ä»£ç æ¥è°ç¨version1:ï¼
# efficiency.py
import timeit
lon1,lat1,lon2,lat2=-72.345,34.323,-61.823,54.826
num=500000 #è°ç¨50ä¸æ¬¡
t=timeit.Timer("v1.great_circle(%f,%f,%f,%f)"%(lon1,lat1,lon2,lat2),
"import version1 as v1")
print('纯pythonçæ¬ç¨æ¶:'+str(t.timeit(num))+'sec')
好çï¼æççµèè±äºå¤§çº¦3.3sï¼æç¹æ人ãå½ç¶æ们çæ¶é´å¯è½å¹¶ä¸ä¸æ ·ï¼ä½æ ä¸ä¾å¤é½ä¸æä¹å¿«ã
Python+Cçæ¬
ä¸å®ç¨åº¦ä¸æ··æPythonåCæ°æ®ç±»åï¼æ¯æ们çversion2ï¼ä½è¦æ³¨æï¼å¿
é¡»æ¯.pyxæ件ï¼å 为æ们éè¦å°å
¶ç¼è¯ä¸ºPythonæå±ï¼è¿ä¸æ¥æä¼å¨åé¢ä»ç»è®²ãæ们åæ ·ä¼è°ç¨50ä¸æ¬¡ï¼å¹¶æµå®å®æéçæ¶é´ï¼
# version2.pyx
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 c*radius
å®ç°ä»¥ä¸ä»£ç æ¥è°ç¨version2ï¼
# efficiency.py
import timeit
lon1,lat1,lon2,lat2=-72.345,34.323,-61.823,54.826
num=500000 #è°ç¨50ä¸æ¬¡
t=timeit.Timer("v2.great_circle(%f,%f,%f,%f)"%(lon1,lat1,lon2,lat2),
"import version2 as v2")
print('python+cçæ¬ç¨æ¶:'+str(t.timeit(num))+'sec')
æççµèè±äºå¤§çº¦2.2sï¼ä¸3.3sç¸æ¯æ¯ä¸æ¯å¿«äºä¸å°ãç¸ä¿¡å¨ä½ ççµèä¸ä¹è½å¤çå°è¿ä¹ææ¾çååã
Cçæ¬ï¼Pythonè°ç¨ï¼
æ们大è´åæä¸ä¸ï¼version2çç¶é¢æ¯å¨åªéï¼æ们è°ç¨çæ¯pythonçmath模åï¼æ¯ä¸æ¯è¿é大大å°éå¶äºæ§è½ï¼ç°å¨æ们使ç¨Cæ ååºæ¿ä»£ä¹ï¼
# version3.pyx
cdef extern from "math.h":
float cosf(float theta)
float sinf(float theta)
float acosf(float theta)
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-lat1)/(x)
b=(90-lat2)/(x)
theta=(lon2-lon1)*(x)
c=acosf((cosf(a)*cosf(b))+(sinf(a)*sinf(b)*cosf(theta)))
return radius*c
å®ç°ä»¥ä¸ä»£ç æ¥è°ç¨version3ï¼
# efficiency.py
import timeit
lon1,lat1,lon2,lat2=-72.345,34.323,-61.823,54.826
num=500000 #è°ç¨50ä¸æ¬¡
t=timeit.Timer("v3.great_circle(%f,%f,%f,%f)"%(lon1,lat1,lon2,lat2),
"import version3 as v3")
print('纯cçæ¬(Pythonå½æ°è°ç¨)ç¨æ¶:'+str(t.timeit(num))+'sec')
0.6sï¼ç¸å½æ人ï¼ï¼è¿ææ¯æ们追æ±çé度ä¸æ¯åï¼
Cçæ¬ï¼Cè°ç¨ï¼
è§å¯ä¸é¢ç代ç ï¼å®¹æåç°è°ç¨50ä¸æ¬¡è¿ä¸ªæ¯å¾ªç¯ä½¿ç¨Pythonå®ç°çãæ们ç¥é循ç¯æ¯ä¸ä¸ªç¸å½èæ¶çæä½ï¼é£ä¹å¦ææ们æè¿ä¸ªå¾ªç¯æ¾å°C代ç éï¼æ¯å¦è½æ´è¿ä¸æ¥å°æåæ§è½ï¼
# version4.pyx
cdef extern from "math.h":
float cosf(float theta)
float sinf(float theta)
float acosf(float theta)
cdef float _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-lat1)*(x)
b=(90-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 lat1,float lon2,float lat2,int num):
cdef int i
cdef float x
for i from 0<=i<num:
x=_great_circle(lon1,lat1,lon2,lat2)
return x
å®ç°ä»¥ä¸ä»£ç æ¥è°ç¨version4ï¼
# efficiency.py
import timeit
lon1,lat1,lon2,lat2=-72.345,34.323,-61.823,54.826
t=timeit.Timer("v4.great_circle(%f,%f,%f,%f,%i)"%(lon1,lat1,lon2,lat2,num),
"import version4 as v4")
print('纯cçæ¬(Cå½æ°è°ç¨)ç¨æ¶:'+str(t.timeit(1))+'sec')
æåå°åç°ï¼æ们ææ§è½æåå°äº0.12sï¼é度æé«äºå°è¿30åï¼very amazingã
float great_circle(float lon1,float lat1,float lon2,float lat2){
float radius=3956.0;
float pi=3.14159265;
float x=pi/180.0;
float a,b,theta,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;
clock_t start, finish;
double Total_time;
start = clock();
for(i=0;i<=NUM;i++)
x=great_circle(-72.345,34.323,-61.823,54.826);
finish = clock();
Total_time = (double)(finish-start) / CLOCKS_PER_SEC;
printf("%f sec",Total_time);
printf("\n");
printf("%f",x);
å½ç¶ï¼å¦æä½ æCè¯è¨çéæç¯å¢ï¼ç´æ¥è¿è¡å°±è½å¯ä»¥å¾å°ç»æï¼æ们ç¥éLinuxç³»ç»çgccç»ä»¶è½å¤ç¼è¯C代ç ï¼ä¸ºäºæ¹ä¾¿ï¼æ们ç´æ¥å¨Linuxç³»ç»ä¸ç¼è¯è¿è¡ï¼è¿éææä¾ä¸¤ç§æ¹å¼ï¼
->Linuxç¯å¢ä¸:gcc -lm -octest version5.c
   å½åè·¯å¾çæctest.exe
->time ./ctest
   æµè¯è¯¥æ¨¡åè¿è¡æéæ¶é´
对äºä½¿ç¨Cygwinæ¥å®æè¿æ¡å½ä»¤çç¨æ·ï¼å¯è½ä¼éå°ä¸é¢ç麻ç¦ï¼å主ä¹éå°äº
<center><font color="red">é误ï¼</font>pyconfig.h No such file or directory
</center>
éè¿ç½ä¸æ¥é
ç¸å
³èµæï¼åç°è¿æ¯ç±äºæäºç»ä»¶ç缺失é æçï¼å®ä»¬å¯è½æ¯ä¸é¢è¿äºç»ä»¶ï¼
python-develï¼å¯¹åºpython2.xï¼/python3-develï¼å¯¹åºpython3.xï¼
libxml2-dev
libxslt-dev
ä¸è¬æ¥è¯´ï¼ä¸»è¦æ¯python-develçåå ãæ»ä¹ï¼çæ
åµå§ã
->Linuxç¯å¢ä¸:gcc -o version5 version5.c
   å½åè·¯å¾çæversion5.exe
->./version5
   æµè¯è¯¥æ¨¡åè¿è¡æéæ¶é´
å¯è½ç±äºæµè¯çæ¹å¼ä¸åï¼è¿éç¨æ¶åèè¿è¦é¿é£ä¹ä¸ç¹ãä½æ»çæ¥è¯´ï¼Cythonè½å¤ææå°æ¹åæ§è½ãå½ç¶ï¼å¤§å¤æ°æ
åµä¸ï¼Pythonçæ§è½æ¯è¶³å¤å¥½çï¼ä¸æ¦å¾ªç¯ãæ°åè¿ç®åPythonå½æ°è°ç¨ä¸å»äºï¼æ§è½å°±ä¼ç¸åºå°ä¸éï¼å¨è¿ç§æ
åµä¸ï¼æå»ºè®®ä½ ä»¬ä½¿ç¨Cythonè¿è¡ä¼åã
对äºLinuxç³»ç»ï¼ç¼è¯ä¸ä¸ä»£ç è½å¤å¾å°ï¼.pyx->.c->.o->.dll
è¿æ¯å¯¹åºçï¼å 为Windowsç³»ç»è½å¤ä½¿ç¨çPythonæå±æ¯.pydæ件ï¼èLinuxç³»ç»è½å¤ä½¿ç¨çPythonæå±æ¯.oæ.dllæ件ãä½æ¯æ们å®å
¨ä¸ç¨æ
å¿ï¼ä½ çç³»ç»æ»æ¯æéæ©å°å»åéåå®èªå·±çäºæ
ï¼æ们åªéè¦é¡ºæ°´æ¨èã
#setup.py
# Run as:
# python setup.py build ç¼è¯
# python setup.py install å®è£
ï¼ææåpip install xxxï¼
from distutils.core import setup
from Cython.Build import cythonize
#cythonizeï¼ç¼è¯æºä»£ç 为CæC++ï¼è¿åä¸ä¸ªdistutils Extension对象å表
setup(ext_modules=cythonize('XXXXX.pyx'))
æ们å¨åææå°ç.pyxæ件并ä¸è½ç´æ¥ä½ä¸ºPythonæå±ï¼æ们éè¦ç¼åsetup.pyæ¥å¸®å©æ们è·å¾Pythonæå±ãå¨'XXXXX.pyx'ä¸å¡«å
¥.pyxæ件çè·¯å¾ï¼ç¶åæ们å¨shellç¯å¢ä¸ï¼setup.pyçç®å½ä¸ï¼æ§è¡ä»¥ä¸å½ä»¤ï¼
python setup.py build
ä¸è¬æ¥è¯´ï¼æ们è½å¨å½åç®å½ä¸çå°ä¸ä¸ªå为buildçæ件夹ï¼æ们éè¦çPythonæå±å°±å¨éé¢ï¼æ³¨ææ§è¡è¯¥å½ä»¤çpythonçæ¬è¦å¯¹åºã
æ§è¡ä»¥ä¸å½ä»¤ï¼
python setup.py install
å®è£
该模åå°site-packageæ件夹ä¸ã
Cythonç¼è¯ä¸çé®é¢
æä¹æ以æ æ³åæç« æ说çé£æ ·ä¸åéå
¥é¨ï¼å°±æ¯å 为è¿äºä¹±ä¸å
«ç³çé®é¢ï¼è¿éææ±æ»ä¸ä¸æèªå·±éå°çé®é¢ï¼å¸æåæ¥ç人ä¸è¦åæä¸æ ·èµ°å¼¯è·¯ãåçLinuxç¯å¢é®é¢ä¸å¤§ï¼ä¼åºç°é®é¢ä¸»è¦å°±æ¯Windowsç³»ç»åCygwinè¿ç§ä¼ªLinuxç¯å¢ï¼æå°±ä»è¿ä¸¤ä¸ªç¯å¢åºåï¼
Windows
Unable to find vcvarsall.bat
####
è¿æ¶åçæ们ï¼ä¸è¬ç´æ¥å°±æè¿ä¸ªé误贴å°ç¾åº¦æè
googleä¸ï¼é£ä¹æ们ä¼çå°ä¸å å
³äºWindowsä¸Cythonå®è£
çæç¨ã
注æï¼ä¸è¦æç½ä¸è¯´çï¼å®è£
MinGWï¼ç¶åå¨"..pythonå®è£
è·¯å¾...\Lib\distutils"ä¸æ°å»ºä¸ä¸ªdistutils.cfgæ件ï¼å¨è¿æ件éé¢å¶è®¢ç¼è¯å¨ä¸ºmingw32
[build]
compiler=mingw32
ä¸æ¹é¢ï¼æççµèä¸å·²ç»å®è£
äºCygwinï¼ä¸ºä»ä¹éå¾å¦å¤å®è£
ä¸ä¸ªMinGWï¼å¦ä¸æ¹é¢MinGWç¼è¯åºæ¥çä¸è¥¿ï¼å®è£
ä¸äºä¹æä¸å¥½ä½¿çæ¶åï¼çè³ä¼æ æ³ç¼è¯ï¼å³ä½¿ç¼è¯éè¿ï¼å®è£
ä¸äºï¼ä½ å®è£
çPythonæ ååºä¸æ¯ç±mingwç¼è¯çï¼ä½ çæå±å
å´æ¯mingwç¼è¯çï¼å¾é¾è¯´è½å¤å®å
¨å
¼å®¹æè
è´¨éè·å¾ä¸ãï¼å¼ç¨èªåèæç®2ï¼
ä½æ¯åèæç®2ä¸æå°çæ¹æ³å´ä¸æ¯å¾ç®¡ç¨ï¼å¯è½ä¸æ¯å¾éåæçæ
åµå§ãé£ä¹è¦å¦ä½è§£å³è¿ä¸ªé®é¢ï¼è¿ä¸»è¦æ¯å 为æ¶åå°äºæ¯è¾åºå±çé®é¢ï¼ç±äºåºå±ä¸å¯¹pythonæ¯æçä¸è¶³é æçãåèæç®3æåºçæ¹æ³å®ç¾å°è§£å³äºæçé®é¢ï¼
æå¼Visual Studioçå®è£
ç¨åºè¿å
¥å°ä¸é¢ççé¢ï¼éæ©ä¸é¢è¿äºç»ä»¶å®è£
ã
è´å½é误ï¼Python.hï¼æ²¡æé£ä¸ªæ件æç®å½
####
å
¶å®è¯´ç½äºï¼Cygwinä¸æ¥éï¼å¤§å¤æ°æ
åµå°±æ¯æ³åè¯ä½ ï¼ä½ 丫çæåªäºåªäºç»ä»¶æ²¡ä¸è½½ååï¼ï¼
æ¯çï¼åªè¦æ们å®è£
python-develï¼python2.xï¼/python3-develï¼python3.xï¼ï¼ä¾¿åç°é®é¢è¿åè解å¦ï¼
è¿å 天æ¶è·å¾å¤§ï¼å·¨å¼å¿!
项ç®é¾æ¥ï¼ https://github.com/kingboung/Miniproject/tree/master/Cython_accidence
转载请åç¥ï¼ï¼å主个人ç½ç«ï¼http://www.kingboung.me
æç« æä¸å®åçå°æ¹ï¼è¯·çè¨åç¥ï¼è°¢è°¢æçæå们ã