Author: Gerd Isenberg
Date: 09:04:04 07/20/03
Go up one level in this thread
On July 20, 2003 at 08:01:50, Uri Blass wrote:
>The question is if there is a way to optimize loops without writing the same
>commands twice in the code that does the code less readable.
>
>I have in some places in movei things like
>
>while (i<n)
>{
> f(i);
> i++;
>}
>f(n);
>
>
>f is practically some C lines and not a function
>because I use some local varaibles and giving them to
>a function may make the program slower.
>
>The problem is how to avoid writing the lines of f
>twice without doing the code slower.
>
>The following option is slower because I have also
>i++ when i=n
>
>do
>{
> f(i);
> i++;
>}
>while (i<=n);
>
>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);
>
>Uri
Hi Uri,
I can only agree with Dieter, Omid and Ricardo. Keep your code readable.
An additional register increment should not be a performance issue.
Like Dieter already mentioned, decrementing a loop counter may save an addition
compare instruction, due the dec reg already sets the "Zero"-Flag:
dec ecx
jnz copyloop
Anyway, if this really is a hotspot, the loop body is really small and if n-i is
rather low, you may try a switch with complete loop unrolling but one none
predictable branch:
switch (n-i) // sample for 0 >= n-i >= 7
{
case 7: f(i--);
case 6: f(i--);
case 5: f(i--);
case 4: f(i--);
case 3: f(i--);
case 2: f(i--);
case 1: f(i);
case 0: break;
default: assume (0); // msc specific
}
Gerd
This page took 0 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.