ChessUSA.com TalkChess.com
Hosted by Your Move Chess & Games
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C# Performance
Post new topic    TalkChess.com Forum Index -> Computer Chess Club: Programming and Technical Discussions Flat
View previous topic :: View next topic  
Author Message
Richard Allbert



Joined: 19 Jul 2006
Posts: 678

PostPost subject: Re: C# Performance    Posted: Mon Feb 06, 2012 6:29 pm Reply to topic Reply with quote

Hi Mark, Lucas,

Three posts coming up with the basic structure of the whole thing.

After an couple of days of frustration, I'm not really any further. I spent a lot of time testing the functions on their own using structs, references, anything I could think of,
but saw no improvement. In fact, when I changed the board class to a struct, and accessed the pieces without using a method, the perft went from ca. 2300knps to 1800knps.

This was despite single function tests suggesting that fweer method calls and structs by reference was faster.

I'm not sure if I am doing something working with IsAttacked() as pointed out previously, or if I'm missing something generally with the way C# works. I've also spent
a lot of time browsing online documentation, but it hasn't really helped. (apart from making this C++ vs C# discussion look very tame Wink)

MoveGeneration is as follows: (shortened where code is repeated):

Code:

public static void GenerateMoves(Board board, List<Move> moveList)
{
   int pceStartIndex = 0;
   int pceSq = 0;
   if (board.sideToMove == Colour.Dark)
   {
      // bP
      pceStartIndex = BoardConstants.StartIndex[Pieces.bP];
      pceSq = board.pceLst[pceStartIndex++];

      while (pceSq != Squares.OffBoard)
      {
         GenerateBlackPawnMoves(board, moveList, pceSq);
         pceSq = board.pceLst[pceStartIndex++];
      }

      // bN
      pceStartIndex = BoardConstants.StartIndex[Pieces.bN];
      pceSq = board.pceLst[pceStartIndex++];

      while (pceSq != Squares.OffBoard)
      {
         GenerateNonSlideMoves(board, moveList, Pieces.NDirections, pceSq, Pieces.bN);
         pceSq = board.pceLst[pceStartIndex++];
      }

      // bB
      pceStartIndex = BoardConstants.StartIndex[Pieces.bB];
      pceSq = board.pceLst[pceStartIndex++];

      while (pceSq != Squares.OffBoard)
      {
         GenerateSliderMoves(board, moveList, Pieces.BDirections, pceSq, Pieces.bB);
         pceSq = board.pceLst[pceStartIndex++];
      }
      
      // ...........etc for other pieces, then
      
      
      //bK            
      pceStartIndex = BoardConstants.StartIndex[Pieces.bK];
      pceSq = board.pceLst[pceStartIndex];

      Debug.Assert(pceSq != Squares.OffBoard);

      GenerateNonSlideMoves(board, moveList, Pieces.QDirections, pceSq, Pieces.bK);
      if ((board.currentCastlePermission & BoardConstants.permk) != 0)
         GenerateBKSCAMoves(board, moveList);
      if ((board.currentCastlePermission & BoardConstants.permq) != 0)
         GenerateBQSCAMoves(board, moveList);
   
   
   
//................
}
private static void GenerateNonSlideMoves(Board board, List<Move> moveList, int[] moveArray, int from, int pce)
{   
   int moves = 0;
   int toSq = 0;
   int inc = 0;
   while (moveArray[moves] != 0)
   {
      inc = moveArray[moves];
      toSq = from + inc;
      if (CanLandOrTake(board, pce, toSq) == true)
      {
         moveList.Add(new Move(from, toSq, Pieces.NoPiece, board.GetPiece(toSq), Moves.FlagNone));
      }
      moves++;
   }
}

private static void GenerateSliderMoves(Board board, List<Move> moveList, int[] moveArray, int from, int pce)
{
   Debug.Assert(Pieces.PieceIsValid(pce));
   Debug.Assert(Pieces.PieceSlides(pce));

   int moves = 0;
   int toSq = 0;
   int inc = 0;
   while (moveArray[moves] != 0)
   {
      inc = moveArray[moves];
      toSq = from + inc;
      while (CanLandOrTake(board, pce, toSq) == true)
      {
         moveList.Add(new Move(from, toSq, Pieces.NoPiece, board.GetPiece(toSq), Moves.FlagNone));
         if (board.GetPiece(toSq) != Pieces.NoPiece) break;
         toSq += inc;
      }

      moves++;
   }
}

// one of the castle gen moves

private static void GenerateWKSCAMoves(Board board, List<Move> list)
{
   if (board.GetPiece(Squares.G1) == Pieces.NoPiece && board.GetPiece(Squares.F1) == Pieces.NoPiece)
   {               
      list.Add(new Move(Squares.E1, Squares.G1, Pieces.NoPiece, Pieces.NoPiece, Moves.FlagCastled));
   }
}

private static void GenerateWhitePawnMoves(Board board, List<Move> moveList, int frSq)
{
   // one and two squares
   if (board.GetPiece(frSq + Squares.dirN) == Pieces.NoPiece)
   {
      AddWhitePawnMove(frSq, frSq + Squares.dirN, Pieces.NoPiece, moveList);

      if (Ranks.RankBoard[frSq] == Ranks.Rank2 && board.GetPiece(frSq + Squares.dirN + Squares.dirN) == Pieces.NoPiece)
      {
         moveList.Add(new Move(frSq, frSq + Squares.dirN + Squares.dirN, Pieces.NoPiece, Pieces.NoPiece, Moves.FlagNone));
      }
   }

   if (Pieces.PieceColourIs(board.GetPiece(frSq + Squares.dirNW)) == Colour.Dark)
   {
      AddWhitePawnMove(frSq, frSq + Squares.dirNW, board.GetPiece(frSq + Squares.dirNW), moveList);
   }

   if (Pieces.PieceColourIs(board.GetPiece(frSq + Squares.dirNE)) == Colour.Dark)
   {
      AddWhitePawnMove(frSq, frSq + Squares.dirNE, board.GetPiece(frSq + Squares.dirNE), moveList);
   }

   if (board.enPassantSqaure == (frSq + Squares.dirNW) && board.enPassantSqaure != Squares.OffBoard)
   {
      moveList.Add(new Move(frSq, frSq + Squares.dirNW, Pieces.NoPiece, Pieces.NoPiece, Moves.FlagEnPassant));
   }

   if (board.enPassantSqaure == (frSq + Squares.dirNE) && board.enPassantSqaure != Squares.OffBoard)
   {
      moveList.Add(new Move(frSq, frSq + Squares.dirNE, Pieces.NoPiece, Pieces.NoPiece, Moves.FlagEnPassant));
   }

}

private static void AddWhitePawnMove(int frSq, int tosq, int cap, List<Move> list)
{
   if (Ranks.RankBoard[tosq] == Ranks.Rank8)
   {
      list.Add(new Move(frSq, tosq, Pieces.wQ, cap, Moves.FlagNone));
      list.Add(new Move(frSq, tosq, Pieces.wR, cap, Moves.FlagNone));
      list.Add(new Move(frSq, tosq, Pieces.wB, cap, Moves.FlagNone));
      list.Add(new Move(frSq, tosq, Pieces.wN, cap, Moves.FlagNone));
   }
   else
   {
      list.Add(new Move(frSq, tosq, Pieces.NoPiece, cap, Moves.FlagNone));
   }
}
            


And, if that wasn't enough for sore eyes, some of the is attacked()....

Code:

public static bool SqIsAttackedBySide(int sq, int side, Board board)
{
   if (side == Colour.Light)
   {
      return AttackedByLight(sq, board);
   }
   else
   {
      return AttackedByDark(sq, board);
   }
}     

private static bool AttackedByDark(int sq, Board board)
{
int pceStartIndex = 0;
int deltaFromTo = 0;
int attackersSq = 0;
int defSq = 0;
int inc = 0;

// bP
pceStartIndex = BoardConstants.StartIndex[Pieces.bP];
attackersSq = board.pceLst[pceStartIndex++];

while (attackersSq != Squares.OffBoard)
{
   deltaFromTo = sq - attackersSq;
   if ((AttackConstants.AttackBitArray[deltaFromTo + AttackConstants.DeltaShift] & AttackConstants.bP_Bit) != 0)
   {
      return true;
   }
   attackersSq = board.pceLst[pceStartIndex++];
}

// bN
pceStartIndex = BoardConstants.StartIndex[Pieces.bN];
attackersSq = board.pceLst[pceStartIndex++];

while (attackersSq != Squares.OffBoard)
{
   deltaFromTo = sq - attackersSq;

   if ((AttackConstants.AttackBitArray[deltaFromTo + AttackConstants.DeltaShift] & AttackConstants.N_Bit) != 0)
   {
      return true;
   }
   attackersSq = board.pceLst[pceStartIndex++];
}

// bB
pceStartIndex = BoardConstants.StartIndex[Pieces.bB];
attackersSq = board.pceLst[pceStartIndex++];

while (attackersSq != Squares.OffBoard)
{
   deltaFromTo = sq - attackersSq;

   if ((AttackConstants.AttackBitArray[deltaFromTo + AttackConstants.DeltaShift] & AttackConstants.B_Bit) != 0)
   {
      inc = AttackConstants.AttackStepArray[deltaFromTo + AttackConstants.DeltaShift];

      defSq = attackersSq + inc;
      Debug.Assert(Squares.SquareOnBoard(defSq));

      while (defSq != sq)
      {
         if (board.GetPiece(defSq) != Pieces.NoPiece) break;
         defSq += inc;
      }

      if (defSq == sq) return true;
   }
   attackersSq = board.pceLst[pceStartIndex++];
}


.. which used the attack delta lookup to see if an attack can occure, before looping between the pcesq and target squares to see if the
attacking piece reaches it target

Next post, the makemove..
Back to top
View user's profile Send private message
Display posts from previous:   
Subject Author Date/Time
C# Performance Richard Allbert Fri Jan 27, 2012 8:41 pm
      Re: C# Performance Kevin Hearn Fri Jan 27, 2012 9:01 pm
            Re: C# Performance Richard Allbert Fri Jan 27, 2012 9:13 pm
      Re: C# Performance Gary Fri Jan 27, 2012 9:07 pm
            Re: C# Performance Richard Allbert Fri Jan 27, 2012 9:17 pm
                  Re: C# Performance Gary Fri Jan 27, 2012 10:09 pm
      Re: C# Performance Mark Pearce Sat Jan 28, 2012 12:59 am
            Re: C# Performance Richard Allbert Sat Jan 28, 2012 8:44 am
                  Re: C# Performance Sven Schüle Sat Jan 28, 2012 9:48 am
                        Re: C# Performance Richard Allbert Sat Jan 28, 2012 10:25 am
                              Re: C# Performance Richard Allbert Sat Jan 28, 2012 11:41 am
                  Re: C# Performance Mark Pearce Sat Jan 28, 2012 11:44 am
            Re: C# Performance Richard Allbert Sat Jan 28, 2012 10:31 am
      Re: C# Performance Marco Costalba Sat Jan 28, 2012 1:09 pm
            Re: C# Performance Lucas Braesch Sat Jan 28, 2012 1:52 pm
            Re: C# Performance Richard Allbert Sat Jan 28, 2012 2:05 pm
                  Re: C# Performance Marco Costalba Sat Jan 28, 2012 2:30 pm
                        Re: C# Performance Richard Allbert Sat Jan 28, 2012 5:22 pm
                              Re: C# Performance Sven Schüle Sat Jan 28, 2012 6:02 pm
                                    Re: C# Performance Richard Allbert Sat Jan 28, 2012 6:12 pm
                                    Re: C# Performance Robert Purves Sat Jan 28, 2012 10:17 pm
                              Re: C# Performance Marco Costalba Sat Jan 28, 2012 10:25 pm
                  Re: C# Performance Thomas Petzke Sat Jan 28, 2012 4:21 pm
      Re: C# Performance Richard Allbert Mon Jan 30, 2012 12:52 pm
            Re: C# Performance Marco Costalba Mon Jan 30, 2012 6:37 pm
                  Re: C# Performance Richard Allbert Wed Feb 01, 2012 6:55 pm
                        Re: C# Performance Marco Costalba Wed Feb 01, 2012 7:18 pm
            Re: C# Performance Mark Pearce Mon Jan 30, 2012 9:55 pm
                  Re: C# Performance Richard Allbert Wed Feb 01, 2012 6:57 pm
                        Re: C# Performance Mark Pearce Wed Feb 01, 2012 9:59 pm
            Re: C# Performance Mark Pearce Fri Feb 03, 2012 1:34 am
                  Re: C# Performance Robert Purves Fri Feb 03, 2012 3:26 am
                        Re: C# Performance Mark Pearce Fri Feb 03, 2012 9:28 am
                              Re: C# Performance Sven Schüle Fri Feb 03, 2012 9:45 am
                                    Re: C# Performance Mark Pearce Sat Feb 04, 2012 7:57 pm
                                          Re: C# Performance Sven Schüle Sat Feb 04, 2012 10:36 pm
                                                Re: C# Performance Mark Pearce Sat Feb 04, 2012 11:49 pm
                                                      Re: C# Performance Sven Schüle Sun Feb 05, 2012 12:51 am
                                                            Re: C# Performance Mark Pearce Sun Feb 05, 2012 1:05 am
                              Re: C# Performance Sven Schüle Fri Feb 03, 2012 10:04 am
                                    Re: C# Performance Mark Pearce Fri Feb 03, 2012 11:40 am
                                          Re: C# Performance Richard Allbert Sat Feb 04, 2012 7:10 pm
                                                Re: C# Performance Lucas Braesch Sat Feb 04, 2012 8:31 pm
                                                      Re: C# Performance Mark Pearce Sat Feb 04, 2012 10:23 pm
                                                            Re: C# Performance Mark Pearce Sun Feb 05, 2012 12:23 am
                                                                  Re: C# Performance Lucas Braesch Sun Feb 05, 2012 2:57 am
                                                                        Re: C# Performance Mark Pearce Sun Feb 05, 2012 11:40 am
                                                                              Re: C# Performance Richard Allbert Mon Feb 06, 2012 6:29 pm
                                                                                    Re: C# Performance Richard Allbert Mon Feb 06, 2012 6:29 pm
                                                                                          Re: C# Performance Richard Allbert Mon Feb 06, 2012 6:30 pm
                                                                                          Re: C# Performance Richard Allbert Mon Feb 06, 2012 6:45 pm
                                                                                          Re: C# Performance Gary Mon Feb 06, 2012 7:00 pm
                                                                                          Re: C# Performance Richard Allbert Mon Feb 06, 2012 7:12 pm
                                                                                          Re: C# Performance Sven Schüle Mon Feb 06, 2012 10:50 pm
                                                                                          Re: C# Performance Richard Allbert Tue Feb 07, 2012 9:52 am
                                                                                          Re: C# Performance Richard Allbert Tue Feb 07, 2012 9:53 am
                                                                                          Re: C# Performance Richard Allbert Tue Feb 07, 2012 9:53 am
                                                                                          Re: C# Performance Richard Allbert Tue Feb 07, 2012 9:54 am
                                                                                          Re: C# Performance Mark Pearce Tue Feb 07, 2012 1:59 pm
                                                                                          Re: C# Performance Richard Allbert Tue Feb 07, 2012 2:54 pm
                                                                                          Re: C# Performance Richard Allbert Tue Feb 07, 2012 3:06 pm
                                                                                          Re: C# Performance Mark Pearce Tue Feb 07, 2012 5:17 pm
                                                                                          Re: C# Performance Richard Allbert Tue Feb 07, 2012 6:10 pm
                                                                                          Re: C# Performance Richard Allbert Tue Feb 07, 2012 6:20 pm
                                                                                          Re: C# Performance Mark Pearce Tue Feb 07, 2012 8:02 pm
                                                                                          Re: C# Performance Richard Allbert Tue Feb 07, 2012 9:19 pm
                                                                                          Re: C# Performance Mark Pearce Tue Feb 07, 2012 10:20 pm
                                                                                          Re: C# Performance Richard Allbert Wed Feb 08, 2012 7:47 am
                                                                                          Re: C# Performance Richard Allbert Wed Feb 08, 2012 7:57 am
                                                                                          Re: C# Performance Sven Schüle Wed Feb 08, 2012 10:09 am
                                                                                          Re: C# Performance Mark Pearce Wed Feb 08, 2012 12:16 pm
                                                                                          Re: C# Performance Vincent Diepeveen Sat Feb 18, 2012 10:03 pm
                                                                                          Re: C# Performance Mark Pearce Wed Feb 08, 2012 9:30 am
                                                                                          Re: C# Performance Richard Allbert Wed Feb 08, 2012 12:02 pm
                                                                                          Re: C# Performance Richard Allbert Wed Feb 08, 2012 12:34 pm
                                                                                          Re: C# Performance Mark Pearce Wed Feb 08, 2012 12:56 pm
                                                                                          Re: C# Performance Richard Allbert Wed Feb 08, 2012 1:18 pm
                                                                                          Re: C# Performance Richard Allbert Wed Feb 08, 2012 2:30 pm
                                                                                          Re: C# Performance Richard Allbert Wed Feb 08, 2012 2:34 pm
                                                                                          Re: C# Performance Mark Pearce Wed Feb 08, 2012 3:02 pm
                                                                                          Re: C# Performance Richard Allbert Wed Feb 08, 2012 3:27 pm
                                                                                          Re: C# Performance Richard Allbert Wed Feb 08, 2012 3:29 pm
                                                                                          Re: C# Performance Richard Allbert Wed Feb 08, 2012 3:59 pm
                                                                                          Re: C# Performance Richard Allbert Wed Feb 08, 2012 4:04 pm
                                                                                          Re: C# Performance Mark Pearce Wed Feb 08, 2012 6:07 pm
                                                                                          Re: C# Performance Richard Allbert Thu Feb 09, 2012 10:41 am
                                                                                          Re: C# Performance Mark Pearce Thu Feb 09, 2012 1:47 pm
                                                                                          Re: C# Performance Richard Allbert Thu Feb 09, 2012 2:56 pm
                                                                                          Re: C# Performance Mark Pearce Sat Feb 11, 2012 4:41 pm
                                                                                          Re: C# Performance Richard Allbert Sun Feb 12, 2012 8:34 am
                                                                                          Re: C# Performance Lucas Braesch Sun Feb 12, 2012 10:52 am
                                                                                          Re: C# Performance Richard Allbert Sun Feb 12, 2012 12:26 pm
                                                                                          Re: C# Performance Ron Murawski Mon Feb 13, 2012 5:26 am
                                                                                          Re: C# Performance Lucas Braesch Mon Feb 13, 2012 5:52 am
                                                                                          Re: C# Performance Richard Allbert Mon Feb 13, 2012 6:27 am
                                                                                          Re: C# Performance Ron Murawski Tue Feb 14, 2012 6:38 am
                                                                                          Re: C# Performance Lucas Braesch Tue Feb 14, 2012 8:22 am
                                                                                          Re: C# Performance Mark Pearce Tue Feb 14, 2012 11:30 pm
                                                                                          Re: C# Performance Richard Allbert Thu Feb 16, 2012 10:49 am
                                                                                          Re: C# Performance Mark Pearce Sat Feb 18, 2012 12:34 pm
                                                                                          Re: C# Performance Richard Allbert Mon Feb 20, 2012 9:19 pm
                                                                                          Re: C# Performance Mark Pearce Tue Feb 21, 2012 8:33 am
                                                                                          Re: C# Performance Richard Allbert Tue Feb 21, 2012 6:37 pm
                                                                                          Re: C# Performance Vincent Diepeveen Sat Feb 18, 2012 10:19 pm
                                                                                          Re: C# Performance Mark Pearce Sun Feb 12, 2012 2:01 pm
                                                                                          Re: C# Performance Richard Allbert Sun Feb 12, 2012 4:40 pm
                                                                                          Re: C# Performance Vincent Diepeveen Sat Feb 18, 2012 10:06 pm
                                                                                          Re: C# Performance Mark Pearce Sun Feb 19, 2012 1:01 pm
                                                                                          Re: C# Performance Vincent Diepeveen Sun Feb 19, 2012 5:25 pm
                                                                                          Re: C# Performance Mark Pearce Sun Feb 19, 2012 7:04 pm
                                                                                          Re: C# Performance Vincent Diepeveen Sun Feb 19, 2012 9:17 pm
                                                                                          Re: C# Performance Mark Pearce Sun Feb 19, 2012 11:33 pm
                                                                                          Re: C# Performance Richard Allbert Sun Feb 19, 2012 9:22 pm
                                                                                          Re: C# Performance Vincent Diepeveen Sun Feb 19, 2012 9:32 pm
                                                                                          Re: C# Performance Vincent Diepeveen Sun Feb 19, 2012 9:37 pm
                                                                                          Re: C# Performance Vincent Diepeveen Sun Feb 19, 2012 10:06 pm
                                                                                          Re: C# Performance Lucas Braesch Wed Feb 08, 2012 10:56 am
                                                                                          Re: C# Performance Mark Pearce Wed Feb 08, 2012 11:30 am
                                                                                          Re: C# Performance Richard Allbert Wed Feb 08, 2012 12:04 pm
                                                                                          Re: C# Performance Tony Soares Tue Feb 07, 2012 3:47 am
                                                                                          Re: C# Performance Richard Allbert Tue Feb 07, 2012 9:55 am
                                          Re: C# Performance Mark Pearce Sat Feb 04, 2012 8:37 pm
Post new topic    TalkChess.com Forum Index -> Computer Chess Club: Programming and Technical Discussions

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Powered by phpBB © 2001, 2005 phpBB Group
Enhanced with Moby Threads