Visual C ++-启用优化并浏览优化的代码 [英] Visual C++ - enable optimizations and browse the optimized code

查看:55
本文介绍了Visual C ++-启用优化并浏览优化的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在启用优化的情况下编译我的项目,并查看代码中哪些优化发生了变化.

How I can compile my project with optimizations turned on and see what optimizations changed in my code.

例如:

我的原始代码:

printf("Test: %d",52);
for (int empty=0;i<100000;i++) {
    //Nothing here
}

现在,当我优化编译代码时,我想看看:(我想那样)

Now when I compile my code with optimzations ,I want to see: (I think it'll be like that)

printf("Test: 52");

推荐答案

编译器不会优化修改源代码(您在问题中所显示的内容),而是二进制文件,即由asm指令组成.

The compiler doesn't optimize modify the source code (what you've shown in your question), but the binary, which consists of asm instructions.

打开和关闭优化的方式取决于编译器,因此您必须参考其文档.

How you turn optimization on and off depends on the compiler, so you'll have to refer to its documentation.

在MSVS 中,您可以从顶部工具栏中选择此选项-查找 Debug (未优化)与 Release (已优化).您可以通过使用调试器单步执行代码来查看二进制代码,右键单击->随意显示.

In MSVS, you can choose this option from the top toolbar - look for Debug (un-optimized) vs Release (optimized). You can see the binary code by stepping through the code with the debugger, right click -> show dissasembly.

例如,您的代码生成:

    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
    }

这篇关于Visual C ++-启用优化并浏览优化的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆