improve calculation time [message #88959] |
Tue, 08 July 2014 11:29  |
g.nacarts
Messages: 148 Registered: November 2013
|
Senior Member |
|
|
Hello
I am trying to improve my code in order to make it faster. A part of my code is the following
DIM = make_array(301,301,/DOUBLE)
pval = 0
for i=0, 300 do begin
for j=0, 300 do begin
x = round(i + Bx[i, j])
y = round(j + By[i, j])
if (x lt 301) && (y lt 301) && (x ge 0) && (y ge 0) then begin
DIM[i,j] = SIM[x, y]
endif
pval= pval + (DIM[i,j] - TIM[i, j])^2.
endfor
endfor
Because I repeat the above procedure a lot of times it takes time to run. I noticed that the DIM array it's creating in every single iteration and I supposed that it takes time. I was wondering if I can write it in a different way in order to overwrite it in each iteration instead of delete it and create it all the time. To overwrite it I need to add it into the loop?
Regards,
Gina
|
|
|
Re: improve calculation time [message #88961 is a reply to message #88959] |
Tue, 08 July 2014 13:10   |
Matthew Argall
Messages: 286 Registered: October 2011
|
Senior Member |
|
|
On Tuesday, July 8, 2014 2:29:57 PM UTC-4, g.na...@gmail.com wrote:
> Hello
>
>
>
> I am trying to improve my code in order to make it faster. A part of my code is the following
>
>
>
> DIM = make_array(301,301,/DOUBLE)
>
> pval = 0
>
>
>
> for i=0, 300 do begin
>
> for j=0, 300 do begin
>
> x = round(i + Bx[i, j])
>
> y = round(j + By[i, j])
>
> if (x lt 301) && (y lt 301) && (x ge 0) && (y ge 0) then begin
>
> DIM[i,j] = SIM[x, y]
>
> endif
>
> pval= pval + (DIM[i,j] - TIM[i, j])^2.
>
> endfor
>
> endfor
>
>
>
> Because I repeat the above procedure a lot of times it takes time to run. I noticed that the DIM array it's creating in every single iteration and I supposed that it takes time. I was wondering if I can write it in a different way in order to overwrite it in each iteration instead of delete it and create it all the time. To overwrite it I need to add it into the loop?
>
>
>
> Regards,
>
> Gina
I *think* this is what you want. Make sure to double check...
SIM = randomu(1, 301, 301)
TIM = randomu(8, 301, 301)
DIM = make_array(301,301,/DOUBLE)
pval = 0
Bx = randomu(5, 301, 301)
By = randomu(2, 301, 301)
x = round(rebin(lindgen(1,301), 301, 301) + Bx)
y = round(transpose(rebin(lindgen(1,301), 301, 301)) + Bx)
i_sim = where( (x lt 301) and (y lt 301) and (x ge 0) and (y ge 0), n_sim )
if n_sim gt 0 then DIM[i_sim] = SIM[i_sim]
pval = total( (DIM - TIM)^2 )
|
|
|
|
|
|
|
|
Re: improve calculation time [message #88988 is a reply to message #88987] |
Thu, 10 July 2014 06:46  |
Matthew Argall
Messages: 286 Registered: October 2011
|
Senior Member |
|
|
> I just change the dimensions of my array before. The above are the original one
Yes, you will have to alter my example to fit your data.
In my example, X is originally a 301 element array:
x = indgen(1,301)
In order to add X to Bx and to access SIM and DIM (which are 301x301 arrays), I converted X to a 301x301 array. This allowed me to eliminate FOR loops.
x = lindgen(1,301)
x = round(rebin(x, 301, 301) + Bx)
The error that you mention usually is a result of comparing an array to a scalar
IDL> if bytarr(348,348) eq 0 then print, 'No Error'
% Expression must be a scalar or 1 element array in this context: <BYTE Array[348, 348]>.
|
|
|