Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: optimizing loops question

Author: Uri Blass

Date: 06:36:33 07/20/03

Go up one level in this thread


On July 20, 2003 at 09:11:35, Ricardo Gibert wrote:

>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
>
>Did you profile your code to determine whether optimizing this is worthwhile?

I guess that maybe I can earn 1-2% speed improvement from optimization of it.
I do not know how to profile my code to find how much I can get from it.
I can find time of functions but it does not tell me the information.

>
>Why did you reject "for (;i<=n; i++)  f(i);", which is normal for such cases?

I rejected the last thing because I understand that i get the value n+1 for i in
the end of the for loop and it is the same as the loop that I described:

do
{
  f(i);
  i++;
}
while (i<=n);

Uri



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.