【发布时间】:2014-04-02 19:43:34
【问题描述】:
我是优化新手,需要一些解释...
例如我有下一个程序:
int main() {
for (int i = 0; i < 2000000; i++) {
__asm {
push esi
push edi
inc ebx
inc eax
mov ebx, 0xffffffff
mov eax, 0xaaaaaaaa
pop edi
pop esi
}
}
return 0;
}
所以当我在 vtune 测试中进行 - “高级热点分析”时。我在函数 main 中知道这总是一些热点:
Address Source Line Assembly CPU Time: Total by Utilization CPU Time: Self by Utilization Instructions Retired: Total Instructions Retired: Self Overhead and Spin Time: Total Overhead and Spin Time: Self Wait Time: Total Wait Time: Self Inactive Time: Total Inactive Time: Self
0x401000 Block 1: 0.0% 0.0% 0.0% 0.0%
0x401000 1 push ebx 0.0% 0.0% 0.0% 0.0%
0x401001 1 push esi 0.0% 0.0% 0.0% 0.0%
0x401002 1 push edi 0.0% 0.0% 0.0% 0.0%
0x401003 3 mov ecx, 0x1e8480 0.0% 0.0% 0.0% 0.0%
0x401008 Block 2: 0.0% 0.0% 0.0% 0.0%
0x401008 5 push esi 0ms 0ms 62.3% 14,956,555 0.0% 0ms 0.0% 0ms 0.0% 0ms
0x401009 6 push edi 0.872ms 0.872ms 0.5% 116,752 0.0% 0ms 0.0% 0ms 0.0% 0ms
0x40100a 8 inc ebx 0.0% 0.0% 0.0% 0.0%
0x40100b 9 inc eax 0.0% 0.0% 0.0% 0.0%
0x40100c 10 mov ebx, 0xffffffff 0.0% 0.0% 0.0% 0.0%
0x401011 11 mov eax, 0xaaaaaaaa 0.0% 0.0% 0.0% 0.0%
0x401016 13 pop edi 0.0% 0.0% 0.0% 0.0%
0x401017 14 pop esi 2.923ms 2.923ms 20.6% 4,950,489 0.0% 0ms 8.5% 0.007ms 0.0% 0ms
0x401018 4 dec ecx 0.872ms 0.872ms 6.7% 1,613,429 0.0% 0ms 0.0% 0ms 0.0% 0ms
0x401019 4 jnz 0x401008 <Block 2> 0.0% 0.0% 0.0% 0.0%
0x40101b Block 3: 0.0% 0.0% 0.0% 0.0%
0x40101b 19 pop edi 0.0% 0.0% 0.0% 0.0%
0x40101c 19 pop esi 0.0% 0.0% 0.0% 0.0%
0x40101d 19 xor eax, eax 0.0% 0.0% 0.0% 0.0%
0x40101f 19 pop ebx 0.0% 0.0% 0.0% 0.0%
0x401020 19 ret 0.0% 0.0% 0.0% 0.0%
据我了解,所有指令都是配对的,这对 push/pop 指令的解码没有任何问题... 那么为什么会出现热点并且可以为某些特定的处理器(f.e. i7)删除它们?
感谢您的帮助。
【问题讨论】:
-
插入任意的、毫无意义的内存访问会对你造成影响:)
-
明显的优化是去掉所有的空操作,留下
int main() { return 0; } -
我添加内联只是为了测试...
标签: c optimization assembly