源內容:http://www.cnblogs.com/shouce/p/5558095.html#undefined 下麵的內容是根據“源內容”進行了整改、補充。 三種方式實例化一類,包括無參數構造形式與有參數構造形式性能測試。 測試環境: 1) Visual studio 2015 Communi ...
源內容:http://www.cnblogs.com/shouce/p/5558095.html#undefined
下麵的內容是根據“源內容”進行了整改、補充。
三種方式實例化一類,包括無參數構造形式與有參數構造形式性能測試。
- 使用new關鍵字創建類實例(常用方式)。
- 使用Activator激活器類創建類實例(Activator用以在本地或從遠程創建對象類型,或獲取對現有遠程對象的引用)。
- 使用Assembly程式集創建類實例(Assembly表示一個程式集,它是一個可重用、無版本衝突並且可自我描述的公共語言運行庫應用程式構造塊。該類可以載入程式集、瀏覽程式集的元數據和構成部分、發現程式集中包含的類型以及創建這些類型的實例)。
測試環境:
1) Visual studio 2015 Community
2) Windows 10 Profession
3) Memory 6G,CPU Inter-Core-i3-3220 @ 3.3GHz
測試代碼如下,分別為介面、介面實現類、實例化類的創建、執行測試方法
/// <summary> /// 職業、行業 /// </summary> public interface IProfession { string Name { get; } string MostMeaningThing(); } /// <summary> /// 工人、藍領 /// </summary> public class Worker : IProfession { public Worker(string name) { Name = name; } public Worker() { } public string Name { get; private set; } public string MostMeaningThing() { return "挖掘機哪家強山東找藍翔"; } } public class InstanceClass { //可根據自己的項目名稱進行修改 private string className = "TestProblem.Worker"; //執行次數 private int time = 100000; /// <summary> /// 是否執行有參數構造函數 /// </summary> private bool hasParameters; public InstanceClass(bool hasParameters) { this.hasParameters = hasParameters; } /// <summary> /// 通過new關鍵字創建 /// </summary> public void CreateByNew() { IProfession profession; Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < time; i++) { profession = hasParameters ? new Worker("工人-New-" + i) : new Worker(); } watch.Stop(); Console.Write(watch.ElapsedMilliseconds.ToString().PadLeft(5)); } /// <summary> /// 通過Activator控制類創建 /// </summary> public void CreateByActivator() { Type type = Type.GetType(className); IProfession profession; Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < time; i++) { object obj = hasParameters ? Activator.CreateInstance(type, "工人-Activator-" + i) : Activator.CreateInstance(type); profession = obj as IProfession; } watch.Stop(); Console.Write(watch.ElapsedMilliseconds.ToString().PadLeft(5)); } /// <summary> /// 通過Assembly控制類創建 /// </summary> public void CreateByAssembly() { Assembly assembly = Assembly.GetAssembly(Type.GetType(className)); IProfession profession; Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < time; i++) { object obj = hasParameters ? assembly.CreateInstance(className, true, BindingFlags.Default, null, new object[] { "工人-Assembly-" + i }, CultureInfo.CurrentCulture, null) : assembly.CreateInstance(className); profession = obj as IProfession; } watch.Stop(); Console.Write(watch.ElapsedMilliseconds.ToString().PadLeft(5)); } /// <summary> /// For迴圈,替代代碼中多次手寫類似的For迴圈代碼 /// </summary> /// <param name="time">迴圈次數</param> /// <param name="action">委托</param> public static void ForLoop(int time, Action<int> action) { for (int i = 0; i < time; i++) { action(i); } } /// <summary> /// For迴圈,封裝For迴圈代碼執行 /// </summary> /// <param name="time">迴圈次數</param> /// <param name="action">委托,函數指針</param> public static void ForLoop(int time, Action action) { for (int i = 0; i < time; i++) { action(); } } } /// <summary> /// 執行十次的十萬個創建類實例 /// </summary> private static void ExecuteLakh() { bool hasParameters = true; Console.WriteLine("實例化一個類({0}參數構造)的性能比較(單位:毫秒)", hasParameters ? "有" : "無"); Console.Write("\t\t\t"); InstanceClass.ForLoop(10, i => Console.Write("{0:G}", (i + 1).ToString().PadLeft(5))); Console.WriteLine(); InstanceClass instanceClass = new InstanceClass(hasParameters); Console.Write("CreateByNew".PadRight(24)); InstanceClass.ForLoop(10, () => instanceClass.CreateByNew()); Console.WriteLine(); Console.Write("CreateByActivator".PadRight(24)); InstanceClass.ForLoop(10, () => instanceClass.CreateByActivator()); Console.WriteLine(); Console.Write("CreateByAssembly".PadRight(24)); InstanceClass.ForLoop(10, () => instanceClass.CreateByAssembly()); Console.WriteLine(); }
測試結果如下: