KYLChiu / sporkfish

Chess engine in Python
MIT License
5 stars 0 forks source link

[Investigate] Board switching -slow #67

Closed KYLChiu closed 6 months ago

KYLChiu commented 8 months ago

ncalls tottime percall cumtime percall filename:lineno(function) 254103 8.220 0.000 22.513 0.000 evaluator.py:873(evaluate) 17411446 5.067 0.000 9.103 0.000 init.py:725(piece_at) 17411318 4.558 0.000 14.572 0.000 board_py_chess.py:109(piece_at) 18221628 3.290 0.000 3.290 0.000 init.py:735(piece_type_at) 270052 1.188 0.000 2.835 0.000 init.py:2200(push) 1576589 1.115 0.000 2.544 0.000 init.py:1706(generate_pseudo_legal_moves) 6840750 0.911 0.000 0.911 0.000 piece.py:14(init) 12264738 0.864 0.000 0.864 0.000 evaluator.py:897() 3063713 0.691 0.000 0.806 0.000 init.py:314(scan_reversed) 1510784 0.689 0.000 4.759 0.000 init.py:3582(generate_legal_moves)

Notice that piece wrapping id 4.5 seconds itself. The code is just

         def piece_at(self, square: Square) -> Optional[Piece]:
        """
        Get the piece at the specified square.

        :param square: The target square.
        :type square: int
        :return: The piece at the specified square, or None if the square is empty.
        :rtype: Optional[Piece]
        """

        return (
            Piece(piece.piece_type, piece.color)
            if (piece := self.board.piece_at(square))
            else None
        )

but due to sheer amount of calls, this becomes a problem.

KYLChiu commented 8 months ago

Even removing Move and Piece classes, still slow - but not crazily. Lost 3 seconds.