Re: Inherent inefficiency in domestic "for" loop?



In article <Xns97EED4FE9B37fgothamNOSPAM@xxxxxxxxxxxxxx>,
Frederick Gotham <fgothamNO@xxxxxxxx> wrote:

Yes, exactly. But about 90% of the time, we're dealing with a loop which
will always execute at least once.

I just grepped the for loops in a fairly substantial piece of code
that I wrote. About a third of the for loops can execute zero times.
But almost all of the loops that execute at least once are
initializing data, and are only executed once each. So the proportion
of at-least-once loops executed is probably very small.

For this 90% of the time, there's one
extra redudant evaluation performed.

The vast majority of the at-least-once loops were cases where the
initial value and the valued tested against were constant, so the
compiler can generate code that tests at the end if that is more
efficient. Don't assume that something is inefficient without
considering how it can be implemented.

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.

Perhaps the authors of C had a better insight into what the common case
is and what the costs are.

-- Richard
.



Relevant Pages

  • Re: why is halting problem profound?
    ... answers (that is humans have come up with a solution (an algorithm ... ANY and ALL possible loops of code will ever end, unless you execute the ... 'The halting problem is unsolvable' is profound ...
    (sci.logic)
  • Re: problem with mdlOutput in Simulink
    ... I am running in normal mode. ... My code now have 8 for loops that have to execute for 13 ... I obtained my inputs for my S-function from 28 Discrete ...
    (comp.soft-sys.matlab)
  • Counter Intuitive Results: Optimising an FFT routine for Speed
    ... performed as necessary to match the variables data types. ... BTW - I could not get CYCLE.h to work, Turbo-C does not support ... FFT_NRC starting to execute 5000 loops ...
    (comp.lang.c)
  • Re: problem with mdlOutput in Simulink
    ... My code now have 8 for loops that have to execute for 13 ... I obtained my inputs for my S-function from 28 Discrete ... voltage and current harmonic components. ...
    (comp.soft-sys.matlab)
  • Re: old Basic benchmark
    ... circa 1982 and 1983. ... As it stands it's not compatible with BASICs that execute FOR...NEXT ... loops at least once. ...
    (comp.lang.basic.misc)