【发布时间】:2011-05-14 18:20:01
【问题描述】:
您认为函数 haswon(见下文)还有优化空间吗?
我认识到将参数类型从 __int64 更改为 unsigned __int64 会使函数更快,因此我认为也许还有优化的机会。
更详细: 我正在写一个connect four 游戏。最近我使用了 Profiler Very Sleepy,发现函数 haswon 占用了很多 CPU 时间。该函数为一个玩家使用连接四板的位板表示。我在fourstones 基准测试的源代码中找到了函数本身。位板表示如下:
. . . . . . . TOP
5 12 19 26 33 40 47
4 11 18 25 32 39 46
3 10 17 24 31 38 45
2 9 16 23 30 37 44
1 8 15 22 29 36 43
0 7 14 21 28 35 42 BOTTOM
功能:
// return whether newboard includes a win
bool haswon(unsigned __int64 newboard)
{
unsigned __int64 y = newboard & (newboard >> 6);
if (y & (y >> 2 * 6)) // check \ diagonal
return true;
y = newboard & (newboard >> 7);
if (y & (y >> 2 * 7)) // check horizontal -
return true;
y = newboard & (newboard >> 8);
if (y & (y >> 2 * 8)) // check / diagonal
return true;
y = newboard & (newboard >> 1);
if (y & (y >> 2)) // check vertical |
return true;
return false;
}
谢谢!
编辑: CPU 是 x86,32 位架构,我使用的是 Visual Studio 2008 Express Edition 的编译器。优化标志是 /O2 /Oi /GL。
我尝试了 Ben Jackson 建议的函数 haswon2。来自 Microsoft 编译器的程序集,带有发布版本 (/O2 /Oi /GL) 的默认优化标志,几乎没有运行时差异。看起来 VC-Compiler 与 gcc 相比不能利用它不能以严格的顺序评估每个条件。
结果: 哈斯温原件:
来自本杰克逊的haswon2:
编辑2: 哈斯旺大会:
00401A10 mov eax,dword ptr [esp+4]
00401A14 mov ecx,dword ptr [esp+8]
00401A18 push ebx
00401A19 push esi
00401A1A push edi
00401A1B mov edx,eax
00401A1D mov edi,ecx
00401A1F shrd edx,edi,6
00401A23 mov esi,edx
00401A25 shr edi,6
00401A28 and esi,eax
00401A2A and edi,ecx
00401A2C mov edx,esi
00401A2E mov ebx,edi
00401A30 shrd edx,ebx,0Ch
00401A34 shr ebx,0Ch
00401A37 and edx,esi
00401A39 and ebx,edi
00401A3B or edx,ebx
00401A3D je `anonymous namespace'::haswon+35h (401A45h)
00401A3F mov al,1
00401A41 pop edi
00401A42 pop esi
00401A43 pop ebx
00401A44 ret
00401A45 mov edx,eax
00401A47 mov edi,ecx
00401A49 shrd edx,edi,7
00401A4D mov esi,edx
00401A4F shr edi,7
00401A52 and esi,eax
00401A54 and edi,ecx
00401A56 mov edx,esi
00401A58 mov ebx,edi
00401A5A shrd edx,ebx,0Eh
00401A5E shr ebx,0Eh
00401A61 and edx,esi
00401A63 and ebx,edi
00401A65 or edx,ebx
00401A67 jne `anonymous namespace'::haswon+2Fh (401A3Fh)
00401A69 mov edx,eax
00401A6B mov edi,ecx
00401A6D shrd edx,edi,8
00401A71 mov esi,edx
00401A73 shr edi,8
00401A76 and esi,eax
00401A78 and edi,ecx
00401A7A mov edx,esi
00401A7C mov ebx,edi
00401A7E shrd edx,ebx,10h
00401A82 shr ebx,10h
00401A85 and edx,esi
00401A87 and ebx,edi
00401A89 or edx,ebx
00401A8B jne `anonymous namespace'::haswon+2Fh (401A3Fh)
00401A8D mov edx,eax
00401A8F mov esi,ecx
00401A91 shrd edx,esi,1
00401A95 shr esi,1
00401A97 and esi,ecx
00401A99 and edx,eax
00401A9B mov eax,edx
00401A9D mov ecx,esi
00401A9F shrd eax,ecx,2
00401AA3 shr ecx,2
00401AA6 and eax,edx
00401AA8 and ecx,esi
00401AAA or eax,ecx
00401AAC jne `anonymous namespace'::haswon+2Fh (401A3Fh)
00401AAE pop edi
00401AAF pop esi
00401AB0 xor al,al
00401AB2 pop ebx
00401AB3 ret
【问题讨论】:
-
这个函数肯定每次移动运行一次吗?为什么它需要 1 微秒或 1 毫秒?
-
这几乎肯定不需要优化。
-
该函数由 alpha-beta 游戏树搜索中的其他两个函数调用。其他函数是“getMoves”,用于测试胜利或 zugzwang,以及“评估”,用于测试棋盘是否包含胜利。该函数确实被非常频繁地调用。
-
@Christian:我明白了。好吧,我不能对上面的函数提供任何具体的建议,但是你有没有考虑利用这样一个事实,即每一步之后,如果有一行 4,它必须包含新的一块?
-
@Oli 是否被 AI 使用可能很重要。
标签: c++ c optimization 64-bit bit-manipulation