Answers.Body: <p>One thing you can do if your quiescence search is taking too long (more than 90% of your total search time) is limit the maximum depth of the quiescence search. This can be done the same way you would limit a normal search. Another unrelated tip that doesn't make much of a difference: for your eval function you can return a result in centipawns or millipawns so you can use an int for eval instead.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-15T04:16:03.483Z
Answers.LastActivityDate: 2023-08-15T04:16:03.483Z
Answers.UserDisplayName: user35471
Body: <p>Thanks to information provided by a previous answer I added a quiescence search to the end of my negamax search to ensure I am not left with a piece hanging. I think it works however it takes a very long time, how can I change the code to speed it up? Here is my code: (C#)</p>
  <pre><code>private float quiescenceSearch(Board board,float alpha, float beta)
  {
      qCount++;
      //Evaluation without captures
      float stand_pat = Evaluate(board);
      if (stand_pat &gt;= beta) return beta;
      if (stand_pat &gt; alpha) alpha = stand_pat;
      Move[] legalMoves = board.GetLegalMoves();
      foreach (Move move in legalMoves)
      {
          board.MakeMove(move);
          if (move.IsCapture||board.IsInCheck())
          {
              float eval = -quiescenceSearch(board, -beta, -alpha);
              board.UndoMove(move);
              if (eval &gt;= beta) return eval;
              if (eval &lt; alpha) alpha = eval;
          }
          else    board.UndoMove(move);
      }
      return alpha;
  }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-07-25T14:05:28.250Z
Comments.Text: Hi George. If you don't get good answers on here, maybe the Code Review site could be of help to improve your performance.
Comments.UserDisplayName: Tristen Woodruff
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-08-15T19:01:09.113Z
Comments.Text: My first thought is that making all legal moves looks slow. You would want to only make the moves in the qsearch that are actually captures or checks. So ideally only generate those using a special generateForcingMoves function, or at least check whether move is a capture or checking move before making the "expensive" makeMove call. For captures this shouldn't be too hard, if it is difficult for checking moves you can also exclude those from qsearch.
Comments.UserDisplayName: koedem
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-08-25T14:55:13.290Z
Comments.Text: Take care of perpetual check. Usually Quiescent Search does not search up to 3 checks.
Comments.UserDisplayName: LittleCoder
Tags: <engines>
Title: How to improve performance of Quiescence Search?
ThreadText: # How to improve performance of Quiescence Search?
  
  (Asked by: username_0 on 2023-07-24)
  
  Thanks to information provided by a previous answer I added a quiescence search to the end of my negamax search to ensure I am not left with a piece hanging. I think it works however it takes a very long time, how can I change the code to speed it up? Here is my code: (C#)
  private float quiescenceSearch(Board board,float alpha, float beta)
  {
      qCount++;
      //Evaluation without captures
      float stand_pat = Evaluate(board);
      if (stand_pat >= beta) return beta;
      if (stand_pat > alpha) alpha = stand_pat;
      Move[] legalMoves = board.GetLegalMoves();
      foreach (Move move in legalMoves)
      {
          board.MakeMove(move);
          if (move.IsCapture||board.IsInCheck())
          {
              float eval = -quiescenceSearch(board, -beta, -alpha);
              board.UndoMove(move);
              if (eval >= beta) return eval;
              if (eval < alpha) alpha = eval;
          }
          else    board.UndoMove(move);
      }
      return alpha;
  }
  
  **Tags:** engines
  
  > **Comments:**
  > **username_1**: Hi George. If you don't get good answers on here, maybe the Code Review site could be of help to improve your performance.  
  > **username_2**: My first thought is that making all legal moves looks slow. You would want to only make the moves in the qsearch that are actually captures or checks. So ideally only generate those using a special generateForcingMoves function, or at least check whether move is a capture or checking move before making the "expensive" makeMove call. For captures this shouldn't be too hard, if it is difficult for checking moves you can also exclude those from qsearch.  
  > **username_3**: Take care of perpetual check. Usually Quiescent Search does not search up to 3 checks.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_4 on 2023-08-15. Score: 1
  
  One thing you can do if your quiescence search is taking too long (more than 90% of your total search time) is limit the maximum depth of the quiescence search. This can be done the same way you would limit a normal search. Another unrelated tip that doesn't make much of a difference: for your eval function you can return a result in centipawns or millipawns so you can use an int for eval instead.
  
  
  ---
  
  