[Info-vax] decrementing & for loops in C
Johnny Billquist
bqt at update.uu.se
Mon Jan 12 17:24:25 EST 2009
nobody skrev:
> vaxinf at chemie.uni-konstanz.de wrote:
>> Hi,
>>
>> I tried to run one of the example found on Jim Duff's homepage and
>> found a behavior of for loops written in C:
>
>
> Others have commented about the program not working, so I won't.
>
> Another thing you may experience with some compilers is that the result
> will be that both loops go in the same direction no matter what you
> write. The reason beeing that an optimizing compiler may realize that
> you are not doing anyting inside the loop that forces you to do it in a
> special order. Therefore it may choose to turn one of them around in
> order to do the execution faster.
True.
But for this example, the program is doing something inside the loop. A function
call (printf). And since one argument of that function call is the loop
variable, changing the order of the loop will change the order of the function
calls to printf with different values, so an optimizer who did that, would
potentially change the whole programs effect. I doubt anyone would write an
optimizer who did that. :-)
You can only reorder a loop if it is obvious that this won't change the way the
program acts.
Johnny
--
Johnny Billquist || "I'm on a bus
|| on a psychedelic trip
email: bqt at softjar.se || Reading murder books
pdp is alive! || tryin' to stay hip" - B. Idol
More information about the Info-vax
mailing list