Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: optimizing loops question

Author: Dave Gomboc

Date: 18:11:09 07/21/03

Go up one level in this thread


On July 20, 2003 at 08:01:50, Uri Blass wrote:

>I also thought about the following code but
>I do not like if commands and I understood it is better
>to have branchless code.
>
>do
>{
>  f(i);
>  if (i==n)
>   break;
>  i++;
>}
>while (1);

There must be a branch somewhere; whether it is implicit in a for-loop or
explicit with break is not terribly important.  There's nothing wrong with this
chunk of code, and if you would otherwise have to duplicate the real code that
you use f(i) to represent, then it's your best option for code clarity.

I would write it as

    do {
        // here is the code that you are doing
        // over and over throughout the loop
        // that you used f(i) to represent

        if (n == i) break;
        i++;
    } while (1);

but that's just stylistic.  The whitespace before the line with the break will
grab the eye.

Dave



This page took 0.01 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.