Julien Fiore wrote: > Thanks for the psyco information, Serge. > > > 2) Rewrite the code to be vectorized (don't use psyco) Right now your > > code *doesn't* get any speed benefit from numpy > > I do not understand this point. How to rewrite the code ?
vectorized code means one operation work on multiple items. Here is three variants of the most easy part of your function (i'm sure this part is not hot, it's just an example, the hottest part I'm pretty sure is the *whole* openness loop) setup = """ import numpy, array R = 4 winSize= 2*R + 1 cellSizeX = 11 cellSizeY = 23 """ code1 = """ dist=numpy.zeros((winSize,winSize),float) for i in range(winSize): for j in range(winSize): dist[i][j]=numpy.sqrt((cellSizeX*(i-R))**2 +(cellSizeY*(j-R))**2) """ code2 = """ ind = numpy.indices((winSize,winSize)) dist = numpy.sqrt((cellSizeX*(ind[0]-R))**2 +(cellSizeY*(ind[1]-R))**2) """ code3 = """ dist=array.array('f',chr(0)*(4*winSize*winSize)) for i in range(winSize): for j in range(winSize): dist[i*winSize+j]=((cellSizeX*(i-R))**2 +(cellSizeY*(j-R))**2)**0.5 """ The first one is original, the second is vectorized, the last one is using array.array. Here is the timing in microseconds for R=1,2,5,6,50 68.2184467579 95.1533784322 17.6263407779 >>> 173.079790867 108.401514718 39.155870369 >>> 802.235479649 174.977043172 161.933094849 >>> 1126.00120965 206.326781756 221.849145632 >>> 67636.3336681 6736.00415695 12675.9099271 >>> As you see if R >=6 vectorized version is the fastest, otherwise array.array is faster. You should also try psyco + array.array > Do you mean in C ? I didn't mean it, but it's also an option if you need the best speed. You can try pyrex first, before resorting to raw C. Serge. -- http://mail.python.org/mailman/listinfo/python-list