Boxing is the conversion of a value type to a reference type, and unpacking is the conversion of a reference type to a value type, as well as the problem of performance in boxing and unpacking, but this problem is often overlooked in actual projects, which can lead to significant efficiency problems.
using system;using system.collections;using system.collections.generic;using System.diagnostics;namespace collectionsapplication{ /// <summary> /// Packing Unboxing (hashtable arrarylist list<t>) vs / </summary> class Program{ static void main (String[] args) { while (True) { console.writeline ("Cyclenum:"); string strcCycleNum=Console.ReadLine (); int cyclenum= 0; if (!int. TryParse (strccyclenum, out cyclenum)) { console.writeline ("Input invalid!"); continue;} hashtablecost ( Cyclenum); Arrarylistcost (Cyclenum); genericcost (Cyclenum);}} /// <summary> /// HashTable Cost Test /// </sumMary> /// <param name= "CycleNum" > Cycle times </ Param> static void hashtablecost (Int cycleNum ) { stopwatch sw = new stopwatch (); hashtable hs_ Test = new hashtable (); Sw. Start (); for (int i = 0; i < cyclenum; i++) { hs_test.add (i,i);} &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SW. Stop (); consoleinfo (cycleNum, "Hashtablecost ", &NBSP;SW. Elapsedmilliseconds);} /// <summary> /// ArraryList Cost Test /// </summary > /// <param name= "CycleNum" > Cycle times </param > static void arrarylistcost (Int cycleNum) { stopwatch sw = new stopwatch (); arraylist al_ Test = new arraylist (); Sw. Start (); for (int i = 0; i < cyclenum; i++) { al_test.add (i);} &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SW. Stop (); consoleinfo (cycleNum, " Arrarylistcost ", &NBSP;SW. Elapsedmilliseconds);} /// <summary> /// generics Cost testing /// </summary> /// <param name= "CycleNum" > Cycle times </param> static void genericcost (Int cycleNum) { Stopwatch sw = new Stopwatch (); list<int> ltest = new List<int> ( &);NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SW. Start (); for (int i = 0; i < cyclenum; i++) { ltest.add (i);} &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SW. Stop (); consoleinfo (cycleNum, " Genericcost ", &NBSP;SW. Elapsedmilliseconds);} /// <summary> /// Printing Information /// </summary> /// <param name= "Cyclenum" > Cycle times </param> /// <param name= "MethodName" > method name </param> /// <param name= "cost" > Expenses </param> /// static void Consoleinfo (int cyclenum,string methodname,long cost) { console.writeline (methodname + "Cyclenum:" + cyclenum.tostring () + "cost (s)" + cost. tostring ());}}}
For the test results are still very surprised, the loop added 1000 times, 3 are 0 milliseconds, in 10W and 100W times the results of a sharp contrast between the 305ms,86ms,9ms. The gap is not generally large.
To analyze the code:
Hashtable:public virtual void Add (object key, object value);
Boxed 2 times during the add process
Hs_test.add (i, I);
Hashtable.add () will produce 2 boxing times.
public virtual int Add (object value);
Produced a boxing
Al_test.add (i);
Arrarylist.add produces 2 times of boxing.
Because ltest specifies a type (list<int>), it does not produce a boxing
Ltest.add (i);
List<int> does not produce boxing.
You can also analyze the code through IL, which is no longer listed here.
Summarize
In the operation of large amounts of data must pay attention to boxing and unpacking operations, can be used in the generic replacement of the place or the use of generics bar.
In peacetime general project details also need to pay attention to, after all, the details determine success or failure, and then NB frame, also can not withstand this piece of boxing and unpacking bombing.
(PS: The implicit conversion time to be careful, in the conversion to a string when the habit of using ToString (), after all, is not to produce boxing . )
Reference:
Http://www.2cto.com/kf/201411/349779.html
This article comes from the "Ricky's blog" blog, please be sure to keep this source http://57388.blog.51cto.com/47388/1655158
C #-Comparison of Carton unboxing performance (HashTable, Arrarylist, list<t>)