Net: how to calculate the efficiency of a piece of code, net computing efficiency
In.. Net 4.0 and later versions provide a class in the System. in the Diagnostics namespace, this class can be used to calculate the efficiency of codes at both ends with the same execution results, which is very practical in code optimization.
Is the generic efficiency high or low ?? Let's test the Code as follows:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace ConsoleApp1 {class Program {static void Main (string [] args) {Console. writeLine ("\ r \ n"); System. diagnostics. stopwatch watch = new System. diagnostics. stopwatch (); watch. start (); for (int I = 0; I <10000000; I ++) {ShowObj (123);} Console. writeLine ("Number of milliseconds for object method execution" + watch. elapsedMilliseconds); watch. stop () ;}{ System. diagnostics. stopwatch watch = new System. diagnostics. stopwatch (); watch. start (); for (int I = 0; I <10000000; I ++) {ShowGeneric (123);} Console. writeLine ("Number of milliseconds executed by the generic method" + watch. elapsedMilliseconds); watch. stop ();} Console. read ();} public static void ShowObj (object obj) {string str = "123";} public static void ShowGeneric <T> (T t) {string str = "123 ";}}}
After the above execution, we can change the help classes in our program framework to generic ones.