标签:center family enter code 泛型方法 moni common commons val
简介:
方法的执行效率比较,首先考虑的就是执行方法时所耗费的时长,时长越短,效率越好,耗时越长,效率越差。下面就从执行耗时方法考量三种方法的效率。
实现方法:
static void Main(string[] args) { Monitor.Show(); } public static void Show() { Console.WriteLine("****************Monitor******************"); { int iValue = 12345; long commonSecond = 0; long objectSecond = 0; long genericSecond = 0; //普通方法 { Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < 1000000; i++) { ShowInt(iValue); } watch.Stop(); commonSecond = watch.ElapsedMilliseconds; } //Object { Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < 1000000; i++) { ShowObject(iValue); } watch.Stop(); objectSecond = watch.ElapsedMilliseconds; } //泛型 { Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < 1000000; i++) { Show<int>(iValue); } watch.Stop(); genericSecond = watch.ElapsedMilliseconds; } Console.WriteLine("commonSecond={0},objectSecond={1},genericSecond={2}", commonSecond, objectSecond, genericSecond); Console.ReadKey(); } } #region PrivateMethod private static void ShowInt(int iParameter) { //do nothing } private static void ShowObject(object oParameter) { //do nothing } private static void Show<T>(T tParameter) { //do nothing } #endregion
执行结果:
****************Monitor****************** commonSecond=3,objectSecond=9,genericSecond=3
显然,泛型方法执行效率比较高。
标签:center family enter code 泛型方法 moni common commons val
原文地址:https://www.cnblogs.com/wml-it/p/13064094.html