MessyCode
MessyCode

Reputation: 333

Visual C++ - enable optimizations and browse the optimized code

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

For example:

My original 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");

Upvotes: 0

Views: 383

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258548

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.

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.

Your code, for example, generates:

Without optimizations:

    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)  

With optimizations:

    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
    }

Upvotes: 5

Related Questions