Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: question about optimizing loops

Author: Jaime Benito de Valle Ruiz

Date: 13:31:16 08/22/03

Go up one level in this thread


On August 22, 2003 at 16:23:03, Uri Blass wrote:

>On August 22, 2003 at 16:08:39, Matthew Hull wrote:
>
>>On August 22, 2003 at 16:02:56, Uri Blass wrote:
>>
>>>I have  many cases when I want to do something for all the 1's of a number that
>>>is
>>>0-255
>>>
>>>I do something like this if I am sure that the number is not 0(I start with
>>>while if the number can be 0)
>>>
>>>do
>>>{
>>>  j=smallest_power[i];
>>>/*for example smallest_power[34]=1 because 34=2^1+2^5 and 1 is the smallest
>>>power in this representation*/
>>>  f(j);
>>>  i&=i-1;
>>>}
>>>while (i>0);
>>>
>>>I believe that it is not optimal because I calculate information that I do not
>>>need and it is better to have something like
>>>
>>>for (k=0;k<weight[i];k++)
>>>f(power[i][k]);
>>>
>>>I already have the relevant array weight[i] that gives me the number of 1's but
>>>before generating the array power[i][k] that gives the value of the k power of i
>>>I prefer to ask here maybe someone has a better idea.
>>>
>>>Note that I am even not sure if the second idea is faster because I am afraid
>>>that calling array of 2 dimensions may be slower.
>>>
>>>Uri
>>
>>
>>Can't you set up a test to see which is faster?
>>
>>MH
>
>I can do it but I need first to generate the array power[i][k] and maybe there
>is a better idea so I prefer to hear from other people because I believe that
>the task is probably a task that other people also have experience with it.
>
>I believe that espacially programs that use bitboards do loops on all the bits
>of a bitboard and I do not know if they do it by the first way that I discribed
>or by a different way.
>
>When I look at my program the problem is also relevant for the case that i is a
>bitboard when it can be some pawns squares when I want to do a loop and to
>evaluate every pawn.
>
>Uri

Your array for a number between 0-255 could be fine, but for bitboards I believe
that most people will use assembler (BSF/BSR) rather than an array, and then
remove the least significant bit with i&(i-1) before closing the loop.



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.