Vectorizing Code [message #29539] |
Wed, 27 February 2002 14:44  |
Steve Jones
Messages: 1 Registered: February 2002
|
Junior Member |
|
|
Is it possible to vectorize a simple double for-loop?
for i=0,nstate-1 do begin
for j=0,nvec-1 do begin
sa(i,j)=10.^2*exp(-abs(i-j)*dz/h)
endfor
endfor
I tend to write a large number of such loops and my indexes have been
steadily increasing of late... Is there a faster alternative? Thanks
in advance
|
|
|
Re: Vectorizing Code [message #29642 is a reply to message #29539] |
Fri, 01 March 2002 05:40  |
robert.dimeo
Messages: 42 Registered: November 2001
|
Member |
|
|
Hi Steve,
I vectorized it as follows:
u = 1+bytarr(nstate)
v = 1+bytarr(nvec)
d = u#(indgen(nvec)) - (indgen(nstate))#v
sa = 10.^2*exp(-abs(d)*dz/h)
Your way took 0.125 seconds on my pc for nstate = 500 and nvec = 120.
The vectorized way above took 0.03 seconds.
By the way, the rebin method proposed by Kenneth Bowman also took 0.03 seconds.
Hope this helps,
Rob
Steve Jones <no@email.com> wrote in message news:<270220022244527274%no@email.com>...
> Is it possible to vectorize a simple double for-loop?
>
> for i=0,nstate-1 do begin
> for j=0,nvec-1 do begin
> sa(i,j)=10.^2*exp(-abs(i-j)*dz/h)
> endfor
> endfor
>
> I tend to write a large number of such loops and my indexes have been
> steadily increasing of late... Is there a faster alternative? Thanks
> in advance
|
|
|
Re: Vectorizing Code [message #29654 is a reply to message #29539] |
Thu, 28 February 2002 09:26  |
Pavel A. Romashkin
Messages: 531 Registered: November 2000
|
Senior Member |
|
|
Kenneth Bowman wrote:
>
> I used to feel as you, lost among the REBINs and HISTOGRAMS with
> REVERSE_INDICES, but I have read the REBIN tutorial,
> brothers and sisters, and I have seen the light!
Uh, I didn't say that I am intimidated when I see Rebin. It is just
that, like James absolutely correctly noted, on modern high-clock
machines the loops often aren't slower than memory reallocations,
especially when Rebin'ing large sized arrays.
And in the case like this one, I'd believe Rebin helps if I saw some
clocking results. If the difference is more than a few milliseconds, I
admit my defeat :-)
Pavel
|
|
|
Re: Vectorizing Code [message #29659 is a reply to message #29539] |
Thu, 28 February 2002 08:23  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Brother Bowman (k-bowman@null.tamu.edu) writes:
> I used to feel as you, lost among the REBINs and HISTOGRAMS with
> REVERSE_INDICES, but I have read the REBIN tutorial,
> brothers and sisters, and I have seen the light!
Amen, brother!
Just as a reminder to those of you who think
you still have time to live your wicked ways,
you can find both of JD's notorious tutorials
here:
http://www.dfanning.com/documents/tips.html#Tutorials
You are sure to have an epiphany. :-)
Cheers,
David
--
David W. Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438, E-mail: david@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|