help - speeding up a loop [message #92551] |
Mon, 11 January 2016 09:29  |
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 #92558 is a reply to message #92551] |
Wed, 13 January 2016 06:26   |
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   |
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  |
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
|
|
|