[fpc-pascal] for loop vs while loop

DaWorm daworm at gmail.com
Thu Feb 2 20:49:50 CET 2017

On Thu, Feb 2, 2017 at 4:01 AM, Mark Morgan Lloyd <
markMLl.fpc-pascal at telemetry.co.uk> wrote:

> It's interesting that the real (wallclock) and user times are consistently
> in a different sequence. /If/ the user time is to be believed, there's a
> very small advantage to counting down even if the sequence you want is
> ascending (i.e. test3) rather than simply counting up.

Not looking at the assembly either, but I'd assume it's because you can do
a simple test for zero in the count down case, and have to do a compare
against a value and then test the result on count up, which probably takes
an extra instruction or two.  The optimization can sometimes convert
everything to count down as part of the setup of the loop (I know in Delphi
you'd sometimes see loops run the other way in the debugger when there was
no behavioral change in function).  I guess since in your examples the
final contents of the a variable after the loop exits depends on direction,
then it couldn't do this for the count up loops.  Maybe try again with
Inc(a) in the body?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20170202/0cd6f10b/attachment.html>

More information about the fpc-pascal mailing list