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

Home » Public Forums » archive » Re: How to speed up large 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: How to speed up large FOR loops? [message #79804] Tue, 10 April 2012 12:39 Go to next message
Saurav Dhital is currently offline  Saurav Dhital
Messages: 4
Registered: April 2012
Junior Member
Hi Craig, yup, that is a big problem, which I had to correct a while
back. I create a FITS structure of a fixed size at the beginning and
fill it within the FOR loop and trim it at the end.

I only concatenate arrays so as to put them into fields in the FITS
table (for e.g., x = [[x1],[x2],[x3]] and struct.field = x).

One more thing that I don't do: I don't even use the where() function
in my matching as where() searches through the entire array EVERY
single time. If you are matching large arrays, where() ends up being
the biggest bottleneck.

~Saurav
Re: How to speed up large FOR loops? [message #79807 is a reply to message #79804] Tue, 10 April 2012 11:21 Go to previous messageGo to next message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
On Tuesday, April 10, 2012 12:26:27 PM UTC-4, Saurav Dhital wrote:
> Yes. More specifically, I reform arrays and assign them to the new
> FITS structure. This happens a lot.

This is a problem. A pattern like this,
L = [0]
for i = 0, 1000000-1 do L = [L, i]

will run quickly at first and then slow down as L gets longer and longer.

The trick is to pre-allocate your lists like L. If the size of L is not known in advance, then pre-allocate a reasonable fixed size to begin with, and then grow it by factors of 2 as needed (up to a point).

At the end, you will need to trim the unused portion of L.

I do this kind of pattern all the time, and it can be quite efficient.

Craig
Re: How to speed up large FOR loops? [message #79808 is a reply to message #79807] Tue, 10 April 2012 10:23 Go to previous messageGo to next message
Saurav Dhital is currently offline  Saurav Dhital
Messages: 4
Registered: April 2012
Junior Member
David, I am NOT using the REFORM routine, which I now realize could be
my problem. Here is what I do:

;; combine two 2-d arrays ([N1,N2]) such that the output is
[2,N1,N2]
FUNCTION
form_3Darray,a1,a2
return, transpose([[[transpose(a1)]],
[[transpose(a2)]]])
END

I call this as:
>> x = [[x1],[x2],[x3]] & y = [[y1],[y2],[y3]]
> z = form_3Darray([[x]],[[y]])

I have to do this so I can store z in a single field in the FITS
structure.

Thanks,
~S

On Apr 10, 1:13 pm, David Fanning <n...@idlcoyote.com> wrote:
> Saurav Dhital writes:
>> Yes. More specifically, I reform arrays and assign them to the new
>> FITS structure. This happens a lot.
>
> Are you setting the OVERWRITE keyword on your REFORM
> command? You should be!
>
> Cheers,
>
> David
>
> --
> David Fanning, Ph.D.
> Fanning Software Consulting, Inc.
> Coyote's Guide to IDL Programming:http://www.idlcoyote.com/
> Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: How to speed up large FOR loops? [message #79809 is a reply to message #79808] Tue, 10 April 2012 10:13 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Saurav Dhital writes:

> Yes. More specifically, I reform arrays and assign them to the new
> FITS structure. This happens a lot.

Are you setting the OVERWRITE keyword on your REFORM
command? You should be!

Cheers,

David


--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: How to speed up large FOR loops? [message #79810 is a reply to message #79809] Tue, 10 April 2012 09:53 Go to previous messageGo to next message
Fabzi is currently offline  Fabzi
Messages: 305
Registered: July 2010
Senior Member
On 04/10/2012 06:26 PM, Saurav Dhital wrote:
> Yes. More specifically, I reform arrays and assign them to the new
> FITS structure. This happens a lot.

This is probably the problem. Doing out = [out, INDGEN(10)] is very
inefficient in memory.

If you have IDL 8+ I suppose you could use lists. If not (like me), you
may want to use the useful MGcoArrayList from Michael Galloy:

http://docs.idldev.com/idllib/collection/mgcoarraylist__defi ne.html

Cheers,

Fab
Re: How to speed up large FOR loops? [message #79811 is a reply to message #79810] Tue, 10 April 2012 09:26 Go to previous messageGo to next message
Saurav Dhital is currently offline  Saurav Dhital
Messages: 4
Registered: April 2012
Junior Member
Yes. More specifically, I reform arrays and assign them to the new
FITS structure. This happens a lot.
Re: How to speed up large FOR loops? [message #79812 is a reply to message #79811] Tue, 10 April 2012 09:17 Go to previous messageGo to next message
Fabzi is currently offline  Fabzi
Messages: 305
Registered: July 2010
Senior Member
Hi,

Do you concatenate arrays in your loop?

e.g: out = [out, tmp]



On 04/10/2012 05:48 PM, Saurav Dhital wrote:
> Hi,
>
> By necessity, I have to use a long FOR loop (N>~ 300,000). I have
> made the operations inside the loop efficient enough that the first
> 40,000 iterations are relatively fast (~30 mins). Then it slows down
> to a crawl, with the entire process taking>24 hours. I would
> appreciate any advice how to speed this up.
>
> The operation inside the loop involves matching between two (large)
> FITS files and assigning the matches to another (previously created)
> FITS structure. There are no new variable created per iteration, so
> there should be no memory being used (each loop writes over the
> variables anyways). I even create small substructures (e.g.,>
> sub_mystruct = mystruct[0:10]) so that I don't have to index the large
> structures.
>
> Any tip or advice would be much appreciated,
> ~Saurav
Re: How to speed up large FOR loops? [message #79933 is a reply to message #79804] Thu, 12 April 2012 19:41 Go to previous messageGo to next message
Matt Francis is currently offline  Matt Francis
Messages: 94
Registered: May 2010
Member
Have you looked at PROFILER to see where the bottlenecks are? You
might need to temporarily turn parts of the code inside the loop into
function, just to see which bits are using causing problems. I find
the IDL PROFILER remarkably useful, at least compared to the
equivalent tools in other languages I work with. David Fanning (see
comment 5) has a very good PROFILER tutorial on his site if you are
unfamiliar with its use.
Re: How to speed up large FOR loops? [message #79981 is a reply to message #79933] Fri, 20 April 2012 09:48 Go to previous message
Saurav is currently offline  Saurav
Messages: 3
Registered: April 2012
Junior Member
Thanks! I will use th profiler, probably David's as I already use a
lot of his code.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Creating netCDF file
Next Topic: How to speed up large FOR loops?

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

Current Time: Wed Oct 08 13:52:59 PDT 2025

Total time taken to generate the page: 0.00590 seconds