Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: When is a tablelookup more effective?

Author: Jacques Ruiz

Date: 03:22:51 02/10/03

Go up one level in this thread


On February 09, 2003 at 05:41:55, Albert Bertilsson wrote:

Hello. My two cents.

>Recently I tested a thought on how to optimize my InCheck test routine...
>
>When testing if the player king was next to the opponent king after a move I did
>this:
>...
>for (direction = 0 ; direction < 8 ; direction++)
>{
>	pos = square + Direction[direction];
>	if (!(pos & 0x88) && myBoardOpponent[pos] == King)
>		return true;
>}
>...
>
>I thought this could be optimized by using a table lookup to the KingAttacks
>like this:
>...
>if (Bit[myKingPosOpponent] & KingAttacks[myKingPosPlayer]) return true;
>...
>
>No loops, just a two table lookups....
>
>When I tested it, it turned out that the table lookup was slightly SLOWER!?
>
>Are there any general rules to tell when a table lookup is more efficient?
>This is a very simple loop, but it still involves looking at the board eight
>times, so I guess it's quite close the point where a table lookup is more
>effiecient.
>
>System is a Athlon XP 1800+ with SDRAM, if it matters.
>
>/Regards Albert Bertilsson

I'm surprised you report the scan is faster than the table lookup. Considering
that the two kings next to each other is a very rare situation it will need to
perform the 5 to 8 iterations 99% of the time, while the table lookup has a
constant execution time regarless of the position. Did you ran your speed test
only on one position or did you collect data from a set of games? On some
situation it's possible the scan is slightly better than the lookup and bit
test, depending on your compiler and cpu I guess.

As a side note if you want to do a table lookup in this situation, considering
you already have at your disposal the 2 kings square, a probably faster way to
do it would be:
  if( Distance[myKingPosPlayer][myKingPosOpponent] == 1 ) return true;
This assumes you have pre-computed the Distance[][] table. This avoid the
64-bits & operation, and need to access only one array with 2 indexes, instead
of 2 arrays with 2 indexes. I'm not an ASM expert but I think it's a bit faster
this way in that particular case.

In general I think table lookup is preferable to a scan loop. The big advantage
is you have a constant execution time, while a scan loop can be fast in some
situations but slow in others.

Regards.



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.