Detailed description of performance loss of packing and unpacking

Source: Internet
Author: User

Binning converts a reference type to a value type. Otherwise, it is boxed!

The binning and unboxing functions allow conversion between any value of the value type and the value of the Object type to link the value type with the reference type;


[Csharp]
Example: int val = 100;
Object obj = val;
Console. WriteLine ("object value =", obj );

Example: int val = 100;
Object obj = val;
Console. WriteLine ("object value =", obj );
This is a process of packing and converting the value type to the reference type.


[Csharp]
Int val = 100;
Object obj = val;
Int num = (int) obj;
Console. writeLine ("num:", num); <SPAN style = "LINE-HEIGHT: 24px; FONT-FAMILY: arial,, sans-serif; FONT-SIZE: 14px "> </SPAN>

Int val = 100;
Object obj = val;
Int num = (int) obj;
Console. WriteLine ("num:", num );
This is a box-breaking process. It is the process of converting the value type to the reference type and then converting the reference type to the value type.

Obviously, from the principle, we can see that during packing, a brand new reference object is generated, which may result in time loss, that is, reduced efficiency. See the following code:

[Csharp]
Class Program
{
Static void Main (string [] args)
{
/// Time used: 00: 00: 01.6070325 (it has something to do with the efficiency of the computer)
// ArrayList list = new ArrayList ();
// Stopwatch watch = new Stopwatch ();
// Watch. Start ();
// For (int I = 0; I <10000000; I ++)
//{
//// Use the ArrayList set. Each time a number is added, the packing operation is performed.
// List. Add (I );
//}
// Watch. Stop ();
// Console. WriteLine (watch. Elapsed );
// Console. ReadKey ();/
 
 
// Time used: 00: 00: 00.1402388. After a generic set is used, the packing and unpacking operations are eliminated, which greatly improves the performance.
List <int> list = new List <int> ();
Stopwatch watch = new Stopwatch ();
Watch. Start ();
For (int I = 0; I <10000000; I ++)
{
List. Add (I );
}
Watch. Stop ();
Console. WriteLine (watch. Elapsed );
Console. ReadKey ();
}
}

Class Program
{
Static void Main (string [] args)
{
/// Time used: 00: 00: 01.6070325 (it has something to do with the efficiency of the computer)
// ArrayList list = new ArrayList ();
// Stopwatch watch = new Stopwatch ();
// Watch. Start ();
// For (int I = 0; I <10000000; I ++)
//{
//// Use the ArrayList set. Each time a number is added, the packing operation is performed.
// List. Add (I );
//}
// Watch. Stop ();
// Console. WriteLine (watch. Elapsed );
// Console. ReadKey ();/


// Time used: 00: 00: 00.1402388. After a generic set is used, the packing and unpacking operations are eliminated, which greatly improves the performance.
List <int> list = new List <int> ();
Stopwatch watch = new Stopwatch ();
Watch. Start ();
For (int I = 0; I <10000000; I ++)
{
List. Add (I );
}
Watch. Stop ();
Console. WriteLine (watch. Elapsed );
Console. ReadKey ();
}
}
Summary:

1. packing and unpacking must be: Value Type → reference type or reference type → value type.

Object, interface. The value type is an interface that can be implemented.

Personp = new Student (); // This is called implicit type conversion, not packing.

Studentstu = (Student) p; // This is called display type conversion, not unpacking.

Why can int be boxed to the object type, but cannot be boxed to the string or Person type,

This is because the int type is the parent class of the object type.

2. When unpacking, you must use the packing type to unpack the box.

If it is int in case of packing, int must be used for unpacking and double cannot be used.

3. If this type of overload occurs during method overloading, it is not called unpacking or packing.
4. String connection

 

[Csharp]
String s1 = "";
String s2 = "B ";
Int n3 = 10;
Double d4 = 99.9;
String result = string. Concat (s1, s2, n3, d4 );

String s1 = "";
String s2 = "B ";
Int n3 = 10;
Double d4 = 99.9;
String result = string. Concat (s1, s2, n3, d4 );
[Csharp]
<P style = "LINE-HEIGHT: 80%; MARGIN-TOP: 2.88pt; unicode-bidi: embed; DIRECTION: ltr; MARGIN-BOTTOM: 0pt; VERTICAL-ALIGN: baseline "> <SPAN style =" FONT-FAMILY: 'times New Roman '; FONT-SIZE: 8pt "> // string. concat (s1, s2, n3, d4); </SPAN> <SPAN style = "FONT-FAMILY:; FONT-SIZE: 8pt"> check whether the packing has occurred, and the number of times. </SPAN> </P>

// String. Concat (s1, s2, n3, d4); determines whether or not packing is performed and the number of times. Decompile the IL code, for example:
 

Related Article

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.