Re: Philosophy of for loops [message #21505 is a reply to message #21486] |
Tue, 29 August 2000 00:00   |
Martin Schultz
Messages: 515 Registered: August 1997
|
Senior Member |
|
|
Todd Clements wrote:
>
> Hello, again, everyone!
>
> I was just wondering what the general concensus of the "IDL Expert
> Programmers" was on the use of for loops. When I first learned IDL, I
> remember getting from someone or somewhere the mantra "for loops are evil"
> because they take up so much time. Of course, as I learn more and watch
> what goes on in this group, it seems like "for loops are sometimes evil"
> would be a better mantra. The question then becomes, when do they become
> evil?
>
> In response to my thread on summing diagonal elements, Craig said that for
> loops aren't always bad if you can do a lot at once, and his code proves
> that you can have some fast loops.
>
> So what defines a slow loop? Is it having a bunch of accesses to
> sub-elements of arrays? Is it just having a bunch of statments? I suppose
> I could do some tests of my own, and I have a little, but it's much more
> fun to hear what you all have to say on the subject. I wouldn't have seen
> any IDL-ku if I just kept my thoughts to myself!
>
> Todd
I think it is mostly an economic problem. If it takes you 5
minutes to write code with a loop and that code takes 30 minutes
to execute, you can use these 30 minutes to buy and sell stock
and get rich (or poor ;-). If you use these 30 minutes to write
code without a loop which takes 5 minutes to run, you have spent
the same time without the opportunity. But then, if you want to
(or need to) run the code 100000L times, it will take a year if
the only loop is the outer one (for i=0L,99999 do ... - the L is
important!), but almost 6 years in the slow version. Just imagine
how many new releases of IDL you will miss if you run 6 years!
Cheers,
Martin
--
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[
[[ Dr. Martin Schultz Max-Planck-Institut fuer Meteorologie
[[
[[ Bundesstr. 55, 20146 Hamburg
[[
[[ phone: +49 40 41173-308
[[
[[ fax: +49 40 41173-298
[[
[[ martin.schultz@dkrz.de
[[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[
|
|
|