Preface
Recently, reading the book "You Must Know. NET (second edition)" by Wang Tao's great God, well, first worship ....
In the fifth chapter of the book-the type of taste, on boxing and unpacking a very deep feeling, the concept itself believe that every program ape is not unfamiliar, boxing is to convert the value type to a reference type, unboxing is to convert the reference type to a value type (PS: accidentally back the book), also know that the packing and unpacking process will bring performance problems, However, this problem is often overlooked in practical projects, which can lead to significant efficiency problems. The question is how big, anyway I cried.
Simple contrast test
In addition to writing a simple test example, with Hashtable, Arrarylist, list<t> a simple comparison.
Operating environment: windows7_64 (CPU:I5; RAM:6GB).
The code is as follows:
usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Diagnostics;namespacetest{/// <summary> ///Box Unboxing (HashTable arrarylist List<T>) Comparison/// </summary> classProgram {Static voidMain (string[] args) { while(true) {Console.WriteLine ("number of cycles:"); stringStrccyclenum =Console.ReadLine (); intCyclenum =0; if(!int. TryParse (Strccyclenum, outcyclenum)) {Console.WriteLine ("Invalid input! "); Continue; } hashtablecost (Cyclenum); Arrarylistcost (Cyclenum); Genericcost (Cyclenum); } } /// <summary> ///HashTable Overhead Test/// </summary> /// <param name= "Cyclenum" >Number of Cycles</param> Static voidHashtablecost (intcyclenum) {Stopwatch SW=NewStopwatch (); Hashtable hs_test=NewHashtable (); Sw. Start (); for(inti =0; i < Cyclenum; i++) {Hs_test.add (I, I); } SW. Stop (); Consoleinfo (Cyclenum,"Hashtablecost", SW. Elapsedmilliseconds); } /// <summary> ///arrarylist Overhead Test/// </summary> /// <param name= "Cyclenum" >Number of Cycles</param> Static voidArrarylistcost (intcyclenum) {Stopwatch SW=NewStopwatch (); ArrayList al_test=NewArrayList (); Sw. Start (); for(inti =0; i < Cyclenum; i++) {al_test.add (i); } SW. Stop (); Consoleinfo (Cyclenum,"Arrarylistcost", SW. Elapsedmilliseconds); } /// <summary> ///Generic cost Test/// </summary> /// <param name= "Cyclenum" >Number of Cycles</param> Static voidGenericcost (intcyclenum) {Stopwatch SW=NewStopwatch (); List<int> ltest =Newlist<int>(); Sw. Start (); for(inti =0; i < Cyclenum; i++) {ltest.add (i); } SW. Stop (); Consoleinfo (Cyclenum,"Genericcost", SW. Elapsedmilliseconds); } /// <summary> ///Printing Information/// </summary> /// <param name= "Cyclenum" >Number of Cycles</param> /// <param name= "MethodName" >Method Name</param> /// <param name= "cost" >Overhead</param> Static voidConsoleinfo (intCyclenum,stringMethodName,LongCost ) {Console.WriteLine (MethodName+"number of cycles:"+ cyclenum.tostring () +"overhead (milliseconds):"+Cost . ToString ()); } }}
Test results:
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:
//2 boxing hs_test.add (I, I) during the Add process ;
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.
Summary
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 . )
Reading notes-c# packing and unpacking performance