[SciPy-user] Python loops too slow

Ross Williamson Ross.Williamson@usap....
Wed Apr 8 06:12:03 CDT 2009


Hi All

I'm trying to convert some IDL code into Python and am coming across the 
incredibly slow for loop issue in python. Now I detest IDL but is there 
any fancy way to make the following code not use for loops? Sorry if 
this is obvious.

def make_ellgrid(ngrid, reso):

   result = zeros([ngrid, ngrid])

   for i in arange((ngrid / 2)+1):
      for j in arange((ngrid / 2)+1):
         result[j,i] = 2. * pi * sqrt(i ** 2. + j ** 2) / reso / (ngrid*1.0)

   for i in xrange(ngrid):
      result[i,ngrid / 2+1:] = result[i,1:(ngrid / 2)][::-1]
   for i in xrange(ngrid):
      result[ngrid / 2+1:,i] = result[1:(ngrid / 2),i][::-1]

   return result

Cheers

Ross



More information about the SciPy-user mailing list