The answers so far are good critiques of the code at a technical level, but optimizing performance of a chess engine is a separate topic. Without aggressive optimizations at the algorithmic level you will quickly run into a combinatorial explosion and very slow performance, and a number of chess-specific techniques have been studied to help with this problem....


Use the single responsibility principle. Your board class stores a board state, evaluates it, runs the AI, provides a scoring function, stores game state, does profiling, supports undo, handles mouse clicks, produces a list of legal moves, and stores pieces being held by the user. I might have missed something. Make a class that stores a board state. Give ...


How about unrolling the loop a bit, doing two steps per iteration so you don't need to "swap" a and b? (I'm not familiar with Cython, so didn't benchmark): cdef int fib_c(int n): cdef int a = 0 cdef int b = 1 while n > 1: a += b b += a n -= 2 return b if n > 0 else a


Using one less variable seems faster: cdef int fib_c(int n): if n == 0: return 0 cdef int a = 0 cdef int b = 1 for _ in range(n - 1): a, b = b, a + b return b Running your test on my machine: Original = 2.934 s Improved = 1.522 s


Review Imports import operator is being done twice, which is unnecessary. from itertools import combinations, chain: chain is never used, and can be removed. N Choose R As of Python 3.8, the "n choose r" function is built-in and can simply be imported: from math import comb. The function is never used, so may be deleted. Constants time_slots = 24*4*...

Only top voted, non community-wiki answers of a minimum length are eligible