URL: https://linuxfr.org/users/serge_ss_paille/journaux/pythran-0-2-python-peut-il-etre-aussi-rapide-que-du-c Title: Pythran 0.2 : Python peut-il être aussi rapide que du C ? Authors: serge_sans_paille Date: 2013年02月17日T21:09:53+01:00 License: CC By-SA Tags: pythran et python Score: 19 Salut petit journal, toi qui est fan de Pythran :-) Mais oui, tu sais, ce traducteur Python -> C++ pour le prototypage d'algorithmes dans cette merveille d'expressivité et de concision qu'est le Python ! Sinon, je te renvoie à http://pythonhosted.org/pythran/ où tu trouveras un peu de matière à te mettre sous la dent. Tu as sûrement déjà lu ces deux posts un peu vieux mais assez cocasses : - http://morepypy.blogspot.fr/2011/02/pypy-faster-than-c-on-carefully-crafted.html - http://morepypy.blogspot.fr/2011/08/pypy-is-faster-than-c-again-string.html mais sais tu que Pythran commence à générer du code qui tourne presque aussi vite que du C ? Allez un petit cadeau, tiré de la branche compas2013 du dépot https://github.com/serge-sans-paille/pythran.git, pas encore intégrée dans master, mais qui présage du très bon. Le code suivant, qui est un prototype d'algo de [géomatique](http://fr.wikipedia.org/wiki/géomatique "Définition Wikipédia") ```python #pythran export run(float, float, float, float, float, float, float array 2) import math from numpy import zeros def run(xmin, ymin, xmax, ymax, step, range_, t): range_x = int((xmax - xmin )/step) range_y = int((ymax - ymin )/step) print xmax, xmin, range_x print ymax, ymin, range_y print step pt = zeros((range_x, range_y, 3)) "omp parallel for private(i,j,k,tmp)" for i in xrange(range_x): for j in xrange(range_y): pt[i,j,0], pt[i,j,1] = (xmin+step*i)*180/math.pi, (ymin+step*j)*180/math.pi for k in xrange(t.shape[0]): tmp = 6368.* math.acos( math.cos(xmin+step*i)*math.cos( t[k,0] ) * math.cos((ymin+step*j)-t[k,1])+ math.sin(xmin+step*i)*math.sin(t[k,0])) if tmp < range_: pt[i,j,2]+= t[k,2] / (1+tmp) return pt ``` tourne aussi vite après passage dans pythran que son homologue C. Et ça c'est la grande classe, non ? Tu auras noté avec tendresse la petite directive OpenMP, qui permet à cet algo de s'exécuter en parallèle :-)