Performance differences for simple code

Source: Internet
Author: User
Performance ". NET Framework Program design" in a few lines of code, I have to rearrange, written as a function, so the following decompile code looks more convenient:

public void Boxing ()
{
Int32 v = 5;
Console.WriteLine ("{0},{1},{2}", v,v,v);
}
public void Boxing1 ()
{
Int32 v = 5;
Object o = V;
Console.WriteLine ("{0},{1},{2}", O,o,o);
The output of two functions is the same, but which function is better?

The first one is obviously simpler, but the second best in performance is to look at the decompile code:
Boxing function:
. method public Hidebysig instance void Boxing () cil managed
{
Code size:31 Byte (s)
. maxstack 4
. Locals (
Int32 v_0)
l_0000:ldc.i4.5
l_0001:stloc.0
L_0002:ldstr "{0},{1},{2}"
l_0007:ldloc.0
L_0008:box int32
l_000d:ldloc.0
L_000e:box int32
l_0013:ldloc.0
L_0014:box int32
L_0019:call void [Mscorlib]system.console::writeline (String, Object, object, object)
L_001e:ret
}



The BOXING1 function:
. method public Hidebysig instance void Boxing1 () cil managed
{
Code size:23 Byte (s)
. maxstack 4
. Locals (
Int32 v_0,
Object v_1)
l_0000:ldc.i4.5
l_0001:stloc.0
l_0002:ldloc.0
L_0003:box int32
L_0008:stloc.1
L_0009:ldstr "{0},{1},{2}"
L_000e:ldloc.1
L_000f:ldloc.1
L_0010:ldloc.1
L_0011:call void [Mscorlib]system.console::writeline (String, Object, object, object)
L_0016:ret
}


The first function has three boxing operations, the second is only once, and is smaller in both memory and time consumption.


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.