【发布时间】:2014-07-17 08:29:20
【问题描述】:
我正在用 C# 编写一个带有魔法位板的国际象棋引擎,现在速度很慢。从初始位置计算 perft 6(119,060,324 个位置)需要 2 分钟,而其他引擎可以在 1-3 秒内完成。我目前正在使用这种方法来查找位板中所有 1 的索引:
public static readonly int[] index64 = {
0, 47, 1, 56, 48, 27, 2, 60,
57, 49, 41, 37, 28, 16, 3, 61,
54, 58, 35, 52, 50, 42, 21, 44,
38, 32, 29, 23, 17, 11, 4, 62,
46, 55, 26, 59, 40, 36, 15, 53,
34, 51, 20, 43, 31, 22, 10, 45,
25, 39, 14, 33, 19, 30, 9, 24,
13, 18, 8, 12, 7, 6, 5, 63
};
public static List<int> bitScan(ulong bitboard) {
var indices = new List<int>(30);
const ulong deBruijn64 = 0x03f79d71b4cb0a89UL;
while (bitboard != 0) {
indices.Add(index64[((bitboard ^ (bitboard - 1)) * deBruijn64) >> 58]);
bitboard &= bitboard - 1;
}
return indices;
}
这是调用次数最多的方法,我想加快速度。有没有更快的方法来做到这一点?我想返回一个数组而不是一个列表,但由于位数未知(而且我不想要空元素,因为我有很多 foreach 循环),所以我不知道怎么做。
感谢任何建议。
【问题讨论】:
-
您是否尝试过使用 Visual Studios 性能分析器?可能会为您提供更好的线索,以了解您的程序的哪个部分导致了减速。
-
这是和stackoverflow.com/questions/14086854/…一样的家庭作业
标签: c# bit-manipulation bitwise-operators chess bitboard