comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » help - speeding up a loop
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: help - speeding up a loop [message #92559 is a reply to message #92558] Wed, 13 January 2016 07:38 Go to previous messageGo to previous message
Burch is currently offline  Burch
Messages: 28
Registered: December 2013
Junior Member
On Wednesday, January 13, 2016 at 8:26:26 AM UTC-6, Jeff B wrote:
> On Monday, January 11, 2016 at 11:29:42 AM UTC-6, nata wrote:
>> Hi guys,
>>
>> I am trying to implement a circular smooth on an irregular x, y grid.
>> The following loop takes too much time. How do you think I could make it faster?
>>
>> for i=0L, n_rang-1 do for j=0L, n_azim-1 do begin
>>
>> distkm=sqrt((xx-xx[i,j])^2. + (yy-yy[i,j])^2.)
>>
>> ww=where(distkm lt 5.,nn_w)
>> if nn_w gt 0 then data_res[i,j]=total(data[ww]) / nn_w
>>
>> endfor
>>
>> Thank you for your help,
>> nata
>
> There are some quick changes that can be made for modest speed improvements. For example, compare this with the original:
>
> for i=0L, n_rang-1 do for j=0L, n_azim-1 do begin
>
> deltaX = xx - xx[i,j]
> deltaY = yy - yy[i,j]
> distkm_squared=(deltaX*deltaX + deltaY*deltaY)
> ww=where(distkm_squared lt 25.,nn_w)
> if nn_w gt 0 then data_res[i,j]=total(data[ww]) / nn_w
>
> endfor
>
> -Jeff

After firing up IDL and running a few tests, here are my code and results:

nRang = 200
nAzim = 200

xx = (randomu(7.0, [nRang, nAzim]) - 0.5)*1000.0
yy = (randomu(13.0, [nRang, nAzim]) - 0.5)*1000.0


clock = tic('- Original code')
for i=0l, nRang-1 do for j=0l, nAzim-1 do begin
distkm = sqrt((xx-xx[i,j])^2 + (yy-yy[i,j])^2)
ww = where(distkm lt 5.0, nn_w)
endfor
toc, clock


clock = tic('- Without sqrt()')
for i=0l, nRang-1 do for j=0l, nAzim-1 do begin
distkm_squared = (xx-xx[i,j])^2 + (yy-yy[i,j])^2
ww = where(distkm_squared lt 25.0, nn_w)
endfor
toc, clock

clock = tic('- Without sqrt() and rewriting array^2 to be array*array')
for i=0l, nRang-1 do for j=0l, nAzim-1 do begin
deltaX = xx - xx[i,j]
deltaY = yy - yy[i,j]
distkm_squared = (deltaX*deltaX + deltaY*deltaY)
ww = where(distkm_squared lt 25.0, nn_w)
endfor
toc, clock

% Time elapsed - Original code: 25.422446 seconds.
% Time elapsed - Without sqrt(): 18.863389 seconds.
% Time elapsed - Without sqrt() and rewriting array^2 to be array*array: 7.8514180 seconds.


-Jeff
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: regarding IDL implementation of Gabor Filter based Image Feature Extraction and Image Classification
Next Topic: help - cgimage revise x and y positions

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 20:05:50 PDT 2025

Total time taken to generate the page: 0.04577 seconds