Re: Inherent inefficiency in domestic "for" loop?



A for loop is *supposed* to, under
the right conditions, execute 0 iterations.

Yes, exactly. But about 90% of the time, we're dealing with a loop which
will always execute at least once. For this 90% of the time, there's one
extra redudant evaluation performed.

Unless the compiler unrolls it. And you haven't demonstrated that
performing the extra redundant evaluation isn't faster. And it is
possible for the compiler to generate code that skips the redundant
comparison if it can determine that it is redundant.

If you want a do while loop, you known where to find it.

I don't think the "for" loop should have been shaped to accomodate the 10%
of the time where we might not want the loop to run at all.

In other words: speed trumps all, incorrectness 10% of the time
is unimportant. And if correctness is unimportant, anything can
run in 0 time and 0 bytes.

If anything,
there should have been another kind of loop availabe, analogous as to how a
"while loop" has a sister "do loop".

Why is this worthwhile? Assuming such a loop WAS available, why
should I spend 1,000,000 comparisons in CPU time re-compiling a
program in order to save one comparison?

Even something like a "do for" loop would be nice:

unsigned i;

do for( i = 0; i != some_value; ++i)
{
/* Body */
}

The first time someone has to debug using a do for () instead of a for(),
you'll probably use up 100 years of savings from that redundant comparison.

Gordon L. Burditt
.



Relevant Pages

  • Re: Need syntax/small footprint help
    ... >But actually this is, I think, redundant. ... it is not inside a loop. ... structures has been pre-conditioned via a sort engine ... in regx then this should work... ...
    (comp.lang.perl.misc)
  • Re: How to open multiple files through single ifstream object
    ... I can not create ifstream objects before ... So I am using loop to handle each file. ... Files are automatically closed, so this is redundant, too, if the function ... you'd have to reset the streamstate, ...
    (microsoft.public.vc.language)
  • Re: One more Deaf Grandma topic
    ... Eliminating redundancy is a good thing - it generally ... makes the program easier to read and update. ... calling gets once before enterting the loop, ... this elsif line has a completely redundant clause. ...
    (comp.lang.ruby)
  • Re: question on synthesis
    ... but compare this (a more common pattern) ... Now the CLK'event is not redundant any more. ... for i=1 to v'high loop ... A synchronous barrel shifter, the shift is done in parallel here. ...
    (comp.lang.vhdl)
  • Safe multithreading in ASP.Net
    ... Code in global.asx Application_start subroutine, ... Dim emails As New ForumEmail ... Do While ContinueNotify 'boolean value set to True so loop will continue ... the thread still continues to execute. ...
    (microsoft.public.dotnet.framework.aspnet)