Reading Notes-C # binning Performance
Preface I recently read the book ". NET (second edition) You must know" by Wang Tao .... In chapter 5 of the book-taste type, I am deeply touched by the section "packing and unpacking". I believe that every programmer is familiar with this concept. "Packing" refers to converting the value type to the reference type, unpacking converts the reference type to the value type (ps: accidentally carrying a book). It also knows that the packaging and unpacking process will bring about performance problems, however, in actual projects, this problem is often ignored, which may cause great efficiency problems. I have been crying. In addition to the work, I wrote a simple test example and compared it with HashTable, ArraryList, and List <T>. Running Environment: Windows7_64 (Cpu: i5; Ram: 6 GB ). The Code is as follows: copy the code using System; using System. collections; using System. collections. generic; using System. diagnostics; namespace Test {/// <summary> /// binning (HashTable ArraryList <T>) comparison /// </summary> class Program {static void Main (string [] args) {while (true) {Console. writeLine ("cycles:"); string strcCycleNum = Console. readLine (); int cycleNum = 0; if (! Int. TryParse (strcCycleNum, out cycleNum) {Console. WriteLine ("invalid input! "); Continue;} HashTableCost (cycleNum); ArraryListCost (cycleNum); GenericCost (cycleNum );}} /// <summary> /// HashTable overhead test /// </summary> /// <param name = "cycleNum"> Number of cycles </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);} sw. stop (); ConsoleInfo (cycleNum, "Hash TableCost ", sw. elapsedMilliseconds );} /// <summary> /// ArraryList overhead test /// </summary> /// <param name = "cycleNum"> Number of cycles </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);} sw. stop (); ConsoleInfo (cycleNum, "ArraryListCost", sw. elapsedMilliseconds);} // <summary>/ // Generic overhead test /// </summary> /// <param name = "cycleNum"> Number of cycles </param> static void GenericCost (int cycleNum) {Stopwatch sw = new Stopwatch (); List <int> lTest = new List <int> (); sw. start (); for (int I = 0; I <cycleNum; I ++) {lTest. add (I);} sw. stop (); ConsoleInfo (cycleNum, "GenericCost", sw. elapsedMilliseconds );} /// <summary> /// print information /// </summary> /// <param name = "cycleNum"> Number of cycles </param> /// <para M name = "methodName"> method name </param> // <param name = "cost"> overhead </param> static void ConsoleInfo (int cycleNum, string methodName, long cost) {Console. writeLine (methodName + "cycles:" + cycleNum. toString () + "overhead (millisecond):" + cost. toString () ;}} copy the code test results: the test results are still surprising. The value of the test results is added 1000 times in a loop, and the values of the three are 0 milliseconds, 305 ms, 86 ms, and 9 ms respectively. The gap is not general. Code Analysis: // HashTable: public virtual void Add (object key, object value); // during the Add process, hs_Test.Add (I, I) is packed twice ); hashTable. add () will generate two times of packing. // Public virtual int Add (object value); // a boxed al_Test.Add (I); ArraryList. add is packed twice. // Because the type (List <int>) specified by lTest does not generate packing lTest. Add (I); List <int> does not generate packing. You can also analyze the code by using IL, which is not listed here. In summary, when operating a large amount of data, you must pay attention to the packing and unpacking operations. You can use generics instead. Pay attention to the details of normal projects. After all, the Details determine success or failure. The NB framework cannot withstand the bombing of this piece of packing and unpacking. (Ps: Be careful when using implicit conversion. ToString () is used when converting to a string. After all, it does not produce packing .)