优化修改源代码(您在问题中显示的内容),而是修改由asm指令组成的二进制代码。
如何在启用优化的情况下编译项目,并查看代码中更改了哪些优化。
例如:
我的原始代码:
printf("Test: %d",52);
for (int empty=0;i<100000;i++) {
//Nothing here
}
现在,当我用优化编译代码时,我想看到:(我想它会是这样的)
printf("Test: 52");
编译器不是
如何打开和关闭优化取决于编译器,因此您必须参考其文档。
在MSVS中,您可以从顶部工具栏中选择此选项-查找Debug
(未优化)与Release
(优化)。您可以通过使用调试器遍历代码来查看二进制代码,右键单击->showdissambly。
例如,您的代码生成:
如果不进行优化:
printf("Test: %d",52);
0097171E mov esi,esp
00971720 push 34h
00971722 push offset string "Test: %d" (9788C8h)
00971727 call dword ptr [__imp__printf (97C3E0h)]
0097172D add esp,8
00971730 cmp esi,esp
00971732 call @ILT+575(__RTC_CheckEsp) (971244h)
for (int i=0;i<100000;i++) {
00971737 mov dword ptr [i],0
0097173E jmp wmain+49h (971749h)
00971740 mov eax,dword ptr [i]
00971743 add eax,1
00971746 mov dword ptr [i],eax
00971749 cmp dword ptr [i],186A0h
00971750 jge wmain+54h (971754h)
//Nothing here
}
00971752 jmp wmain+40h (971740h)
通过优化:
printf("Test: %d",52);
013A1000 push 34h
013A1002 push offset string "Test: %d" (13A20F4h)
013A1007 call dword ptr [__imp__printf (13A209Ch)]
013A100D add esp,8
for (int i=0;i<100000;i++) {
//Nothing here
}