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

Home » Public Forums » archive » Re: Is it really more efficient to work with arrays than FOR loops?
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
Re: Is it really more efficient to work with arrays than FOR loops? [message #51493] Fri, 24 November 2006 02:34
Alvin is currently offline  Alvin
Messages: 4
Registered: March 2004
Junior Member
Thanks all!
I will have to battle with it some more. I like Jean's Idea, although
the DT method (link from David's post) seems wonderful. I saw it a
while back, but I don't think I can code the DT in my favour. So I will
have to stick to basics.
Alvin.
Re: Is it really more efficient to work with arrays than FOR loops? [message #51513 is a reply to message #51493] Thu, 23 November 2006 09:13 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Alvin writes:

> I was wondering if it is really that more efficient to work with arrays
> (large ones that is). For example I have the following simple code,
> which takes about 30 min to run:
>
> FOR z=0L, 400 DO BEGIN
> FOR y=0L, 400 DO BEGIN
> FOR x=0L, 400 DO BEGIN
> fn=f(z) ;a function of z
> gn=f(z) ;another function of z
> IF ( f(z) * x + g(z) * y GE f(z) * g(z) ) THEN BEGIN
> blah
> blah
> blah
> ENDIF
> ENDFOR
> ENDFOR
> ENDFOR
>
> Now if I tried to vectorize the above, would it do me any good in
> saving time and possibly memory? If I say something like f(z) # x +
> g(z) # y, where these are all vectors, I have a feeling that I am not
> covering all the possible combinations as the FOR loop above. Does
> anyone have any ideas, or suggestions?

http://www.dfanning.com/code_tips/slowloops.html

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: Is it really more efficient to work with arrays than FOR loops? [message #51514 is a reply to message #51513] Thu, 23 November 2006 08:40 Go to previous message
Jean H. is currently offline  Jean H.
Messages: 472
Registered: July 2006
Senior Member
Alvin wrote:
> Hey all,
> I was wondering if it is really that more efficient to work with arrays
> (large ones that is). For example I have the following simple code,
> which takes about 30 min to run:
>
> FOR z=0L, 400 DO BEGIN
> FOR y=0L, 400 DO BEGIN
> FOR x=0L, 400 DO BEGIN
> fn=f(z) ;a function of z
> gn=f(z) ;another function of z
> IF ( f(z) * x + g(z) * y GE f(z) * g(z) ) THEN BEGIN
> blah
> blah
> blah
> ENDIF
> ENDFOR
> ENDFOR
> ENDFOR
>
> Now if I tried to vectorize the above, would it do me any good in
> saving time and possibly memory?

first, in you example, compute gz and fz only once, in the Z loop (not
in the Y nor X loop!). THen use gz and fz instead of g(z) and f(z) AGAIN
in the X loop... compute fz*gz in the Z loop, compute gz*y in the Y
loop... there is no reason for computing this more than once!


Now YES, it is usually much faster to vectorize your code... especially
with small arrays. I have some functions that are several hundreds time
faster once vectorized with *small* arrays (few hundred thousands) but
the same function becoms dozen of time slower with large arrays (few
dozen of millions).
Memory can become an issue too... with arrays of size 400 it should not
be a problem... apply Paolo's suggestion. However, again, if you have
several millions entries, it might be just too much to do the
multiplication of the arrays..
x=findgen(n)#replicate(1,n)
ind=where( x*a+y*b GE c)

Jean

If I say something like f(z) # x +
> g(z) # y, where these are all vectors, I have a feeling that I am not
> covering all the possible combinations as the FOR loop above. Does
> anyone have any ideas, or suggestions?
> Thanks,
> Alvin.
>
Re: Is it really more efficient to work with arrays than FOR loops? [message #51518 is a reply to message #51514] Thu, 23 November 2006 07:21 Go to previous message
Paolo Grigis is currently offline  Paolo Grigis
Messages: 171
Registered: December 2003
Senior Member
Well, you might want to express x and y as 2-dimensional arrays,
something like

n=400
x=findgen(n)#replicate(1,n)
y=transpose(x)

then you can write condition in the form

ind=where( x*a+y*b GE c)

and compute whatever you want to compute for
x[ind] and y[ind]...

Ciao,
Paolo

Alvin wrote:
> Hey all,
> I was wondering if it is really that more efficient to work with arrays
> (large ones that is). For example I have the following simple code,
> which takes about 30 min to run:
>
> FOR z=0L, 400 DO BEGIN
> FOR y=0L, 400 DO BEGIN
> FOR x=0L, 400 DO BEGIN
> fn=f(z) ;a function of z
> gn=f(z) ;another function of z
> IF ( f(z) * x + g(z) * y GE f(z) * g(z) ) THEN BEGIN
> blah
> blah
> blah
> ENDIF
> ENDFOR
> ENDFOR
> ENDFOR
>
> Now if I tried to vectorize the above, would it do me any good in
> saving time and possibly memory? If I say something like f(z) # x +
> g(z) # y, where these are all vectors, I have a feeling that I am not
> covering all the possible combinations as the FOR loop above. Does
> anyone have any ideas, or suggestions?
> Thanks,
> Alvin.
>
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Thanks!
Next Topic: Julian time problem

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

Current Time: Wed Oct 08 15:12:41 PDT 2025

Total time taken to generate the page: 0.20511 seconds