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 
Switch to threaded view of this topic Create a new topic Submit Reply
help - speeding up a loop [message #92551] Mon, 11 January 2016 09:29 Go to next message
natha is currently offline  natha
Messages: 482
Registered: October 2007
Senior Member
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
Re: help - speeding up a loop [message #92557 is a reply to message #92551] Wed, 13 January 2016 03:42 Go to previous messageGo to next message
greg.addr is currently offline  greg.addr
Messages: 160
Registered: May 2007
Senior Member
Maybe if you set up a regular grid with suitable spacing for the smoothed data - which will likely include far fewer points - and loop through that one point by point? If you need the data back on the original grid, you could sample back from it without a loop.

cheers,
Greg
Re: help - speeding up a loop [message #92558 is a reply to message #92551] Wed, 13 January 2016 06:26 Go to previous messageGo to next message
Burch is currently offline  Burch
Messages: 28
Registered: December 2013
Junior Member
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
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 next 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
Re: help - speeding up a loop [message #92560 is a reply to message #92559] Wed, 13 January 2016 08:57 Go to previous message
natha is currently offline  natha
Messages: 482
Registered: October 2007
Senior Member
Hi Jeff,

Thank you for all your comments and tests! I am impressed to see the difference in computation time between array^2 and array*array.
I will implement this last version for sure... I am also parallelizing the code.

Thank you for all your comments,
nata
  Switch to threaded view of this topic Create a new topic Submit Reply
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 11:44:47 PDT 2025

Total time taken to generate the page: 0.00755 seconds