Think about how to make a program faster (original) by compiling

Source: Internet
Author: User
Tags mscorlib

There are two ways to compile the first phase of a. NET program:
The debug version is primarily easy to debug and analyze.
Release releases are optimized for smaller size, faster execution, and slower compilation.

Smaller, mainly because the release version of the program removes all breakpoints, code lines and other debugging information.
So why is execution faster?

A simple test code (original post, reprint please indicate the source)

with WinDbg Analyze Debug version

1 Normal JIT Generated code2 ConsoleApp.Program.Main ()3Begin 000007fe94260090, size -4Warning:unable to verify Checksum forConsoleApp.exe5 6D:\Study\ConsoleApp\Program.cs @ +:7>>> 000007fe '942600904883ec28 Sub rsp,28h8000007fe '9426009448b848341494fe070000 mov rax,7fe94143448h9 000007fe ' 9426009e 8b00 mov eax,dword ptr [Rax]Ten 000007fe ' 942600a0 85c0 test eax,eax One000007fe ' 942600A27405JE 000007fe ' 942600a9 A000007fe ' 942600a4 e85363a45f call clr!Jit_dbgisjustmycode (000007FE ' f3ca63fc) -000007fe ' 942600a9 -NOP -  theD:\Study\ConsoleApp\Program.cs @ -: -000007fe ' 942600AA b901000000 mov ecx,1 -000007fe ' 942600AF E874bfeeff call 000007fe ' 9414c028 (CONSOLEAPP.PROGRAM.ADD (Int32), MdToken:0000000006000002) -000007fe ' 942600B4 -NOP +  -D:\Study\ConsoleApp\Program.cs @ +: + 000007fe ' 942600b5 66ba2b00 mov dx,2bh A000007fe ' 942600b9 b901000000 mov ecx,1 at000007fe ' 942600be E86dbfeeff call 000007fe ' 9414c030 (ConsoleApp.Program.Calc (Int32, Char), MdToken:0000000006000003) -000007fe ' 942600C3 -NOP -  -D:\Study\ConsoleApp\Program.cs @ A: -000007fe ' 942600c4 8b0d7a35eeff mov ecx,dword ptr [000007fe '94143644] -Warning:unable to verify Checksum forC:\Windows\assembly\NativeImages_v4.0. 30319_64\mscorlib\abf62e6545d2802fc60286678a67e6bf\mscorlib.ni.dll in000007fe ' 942600ca e8b1d2d256 call mscorlib_ni+0xd0d380(000007FE ' eaf8d380) (System.Console.WriteLine (Int32), MdToken:0000000006000990) -000007fe ' 942600CF -NOP to  +D:\Study\ConsoleApp\Program.cs @ at: - 000007fe ' 942600d0 eb00 jmp 000007fe ' 942600d2 the000007fe ' 942600d2 -NOP * 000007fe ' 942600d3 4883c428 add rsp,28h $000007fe ' 942600D7 c3 ret
View Code

with WinDbg Analysis Release version

1 Normal JIT Generated code2 ConsoleApp.Program.Main ()3Begin 000007fe94260090, size in4Warning:unable to verify Checksum forConsoleApp.exe5 6D:\Study\ConsoleApp\Program.cs @ -:7>>> 000007fe '942600904883ec28 Sub rsp,28h8000007fe '94260094Ff05aa35eeff Inc DWORD PTR [000007FE]94143644]9 TenD:\Study\ConsoleApp\Program.cs @ +: One 000007fe ' 9426009a 66ba2b00 mov dx,2bh A000007fe ' 9426009e b901000000 mov ecx,1 -000007fe ' 942600a3 E888bfeeff call 000007fe ' 9414c030 (ConsoleApp.Program.Calc (Int32, Char), MdToken:0000000006000003) -  theD:\Study\ConsoleApp\Program.cs @ A: -000007fe ' 942600a8 8b0d9635eeff mov ecx,dword ptr [000007fe '94143644] -Warning:unable to verify Checksum forC:\Windows\assembly\NativeImages_v4.0. 30319_64\mscorlib\abf62e6545d2802fc60286678a67e6bf\mscorlib.ni.dll -000007fe ' 942600ae e8cdd2d256 call mscorlib_ni+0xd0d380(000007FE ' eaf8d380) (System.Console.WriteLine (Int32), MdToken:0000000006000990) +000007fe ' 942600b3 -NOP - 000007fe ' 942600b4 4883c428 add rsp,28h +000007fe ' 942600B8 c3 ret
View Code

Can be seen:

Size: Debug Version Size 48,release Version Size 29

19 Line: Debug version has additional operations to enter the code block of the Main method (to be researched)

20 Line:Debug version has the overhead of calling the Add method, release version inline processing and Direct Inc COMPUTE

21 Rows: Because the Calc method contains exception handling modules, it cannot be inline, both debug and release versions have the overhead of calling

22 rows: All have the overhead of calling the System method WriteLine

23 Line: Debug version has extra action to leave code block of Main method

In fact, the JIT uses a large number of probing methods to determine whether a method should be inline, although it is not necessary to deliberately make the written method satisfy the inline condition, but reasonable use helps to make the program faster.

Think about how to make a program faster (original) by compiling it

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.