Objective
Recently watching the great God of Wang Tao, "You Must know. NET (second edition)" A book, well, first worship .... in the book of the fifth-taste type, the box and unpacking a very deep feeling, the concept itself believe that every program ape is not unfamiliar, Boxing is converting a value type to a reference type, and unpacking is converting a reference type to a value type (PS: Accidentally and back a book), and knowing that the packing and unpacking process will pose a performance problem, but this problem is often overlooked in real-world projects, which can lead to significant efficiency problems. The question is how big, anyway I cried. Simple contrast test wrote a simple test example in addition to the work, with a simple comparison of Hashtable, Arrarylist and list<t>. Operating Environment: windows7_64 (CPU:I5; RAM:6GB). code below: copy code using system;using system.collections;using system.collections.generic;using System.diagnostics; namespace test{ //<summary> //Packing unboxing (HashTable arrarylist list<t>) Compare //</summary> class program { St atic void Main (string[] args) { while (true) &NB Sp { Console.WriteLine ("Number of Cycles:"); &NB Sp String strccyclenum = Console.ReadLine (); int cyclenum = 0; & nbsp if (!int. TryParse (Strccyclenum, out Cyclenum)) { Console.WriteLine ("Invalid input! "); continue; &NBSP ; } Hashtablecost (cyclenum); Arrarylistcost (cyclenum); Genericcost ( Cyclenum); } } //&L t;summary> //HashTable cost test //</summary> & nbsp //<param name= "Cyclenum"> Cycle times </param> static void hashtablecost (int cyclenum) {&NB Sp Stopwatch SW = new Stopwatch (); Hashtable hs_t EST = new Hashtable (); SW. Start (); for (int i = 0; i < cyclenum; i++) { Hs_test.add (i, i); &NBSP ; } SW. Stop (); Consoleinfo (Cyclenum, "Hashtablecost", SW. Elapsedmilliseconds); } //<summary> &NBS P //arrarylist spending test /</summary> //<param name= " Cyclenum "> Cycle times </param> &NBSP; static void Arrarylistcost (int cyclenum) { Stopwatch s w = new Stopwatch (); ArrayList al_test = new ArrayList (); &NB Sp SW. Start (); for (int i = 0; i < cyclenum; i++) { Al_test.add (i); } SW. Stop (); Consoleinfo (Cyclenum, "Arrarylistcost", SW. Elapsedmilliseconds); } //<summary> &NBS P ///generic cost test //</summary> //<param name= "Cyclenum > Cycle times </param> static void genericcost (int cyclenum) &NBSP { Stopwatch SW = new Stopwatch (); list<int> ltest = new list<int> (); SW. Start (); for (int i = 0; i < cyclenum; i++) { Ltest.add (i); }&N Bsp SW. Stop (); Consoleinfo (Cyclenum, "Genericcost", SW. Elapsedmilliseconds); } //<summary> &NBS P ///printing information //</summary> //<param name= "Cyclenum" &G T; cycle times </param> //<param name= "MethodName" > method name </param> //<param name= "cost" > Overhead </param> static void Consoleinfo (int cyclenum, string methodName, long cost) { Console.WriteLine (methodName + "cycle times:" + cyclenum.tostring () + " Overhead (ms): "+ Cost." ToString ()); } }} copy code test result: test result value for test results still surprised, Loop added 1000 times, 3 of them were 0 milliseconds, and in contrast to the results of 10W and 100W, they were 305ms,86ms,9ms. The gap is not generally large. Analysis of the code: //hashtable:public virtual void Add (object key, Object value), //2 times in 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>) and does not produce a boxing ltest.add (i); List<int> does not produce boxing. can also analyze the code through IL, which is no longer listed here. Summary In the operation of a large number of data must pay attention to boxing and unpacking operations, you can use 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, does not produce boxing. )
Packing and unpacking performance in reading notes-c#