[SciPy-user] Python loops too slow

josef.pktd at gmail.com josef.pktd at gmail.com
Wed Apr 8 09:53:46 EDT 2009


On Wed, Apr 8, 2009 at 9:14 AM, Pauli Virtanen <pav at iki.fi> wrote:
> Wed, 08 Apr 2009 23:12:03 +1200, Ross Williamson kirjoitti:
> [clip]
>>    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)
>
> i = arange(ngrid/2 + 1)
> j = arange(ngrid/2 + 1)
> result = 2*pi*hypot(i[None,:], j[:,None])/reso/ngrid
>

second part can also be vectorized

  half = (ngrid / 2)
  result[:, half:] = result[:, half:0:-1]
  result[half:, :] = result[half:0:-1, :]

Josef



More information about the SciPy-User mailing list