You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

国际象棋AI中Board类复制函数的性能优化方案咨询

国际象棋AI棋盘复制函数的性能优化问题

我正在开发一款国际象棋AI,当前代码的性能瓶颈是棋盘(Board)的复制函数。该函数被调用数十万次,因此我需要它尽可能高效。我当前的实现方式是创建类的新实例并复制所有参数,但这速度太慢,单次执行耗时约0.014ms。请问是否有办法优化这段代码,还是我需要重新设计代码逻辑?感谢帮助!

Board.copy() 实现代码

Piece[] whiteArrCopied = pieceCopy(this.whiteArr);
Piece[] blackArrCopied = pieceCopy(this.blackArr);
Piece[] pieceArrCopied = new Piece[32];
for (int i = 0; i < 16; i++)
{
    pieceArrCopied[i] = whiteArrCopied[i];
}
for (int i = 0; i < 16; i++)
{
    pieceArrCopied[i + 16] = blackArrCopied[i];
}
Board ret = new Board(pieceArrCopied, whiteArrCopied, blackArrCopied);
if (lastMove != null)
    ret.lastMove = lastMove.copy();
else
    ret.lastMove = null;
if (lastMoveBeforeMove != null)
    ret.lastMoveBeforeMove = lastMoveBeforeMove.copy();
ret.capturedInstance = capturedInstance;
if (legalMoves != null)
    ret.legalMoves = copier(legalMoves);
ret.halfMoves = halfMoves;
ret.fullMoves = fullMoves;

pieceCopy() 实现代码

public Piece[] pieceCopy(Piece[] arr)
{
    Piece[] ret = new Piece[arr.Length];
    for (int i=0; i<ret.Length; i++)
    {
        ret[i] = arr[i].copy();
    }
    return ret;
}

Piece.copy() 实现代码

Piece ret = new Piece(loc.ToArray(), this.color == "white", this.type);
ret.two = this.two;
ret.enPassant = this.enPassant;
ret.dead = this.dead;
ret.moved = this.moved;
ret.castle = this.castle;
return ret;

感谢您的任何帮助!

内容的提问来源于stack exchange,提问作者Jack Newport

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 00:14:59