Three ways to instantiate the performance of a class, three ways to one class
Source Content: http://www.cnblogs.com/shouce/p/5558095.html#undefined
The following content is corrected and supplemented according to the "source content.
Three methods of Instantiation one class, including non-parameter constructor and parameter constructor performance test.
Test environment:
1) Visual studio 2015 Community
2) Windows 10 Protocol sion
3) Memory 6G, CPU Inter-Core-i3-3220 @ 3.3 GHz
The test code is as follows:Create and execute test methods for interfaces, interface implementation classes, and instantiation classes respectively.
/// <Summary> /// occupation, industry /// </summary> public interface IProfession {string Name {get;} string MostMeaningThing ();} /// <summary> /// Worker, blue collar /// </summary> public class Worker: IProfession {public Worker (string name) {Name = name ;} public Worker () {} public string Name {get; private set;} public string MostMeaningThing () {return "which excavator is strong in Shandong looking for lanxiang ";}} public class InstanceClass {// you can modify privat according to your project name. E string className = "TestProblem. worker "; // number of executions private int time = 100000; // <summary> // whether a parameter constructor is executed /// </summary> private bool hasParameters; public InstanceClass (bool hasParameters) {this. hasParameters = hasParameters;} // <summary> // create with the new Keyword /// </summary> public void CreateByNew () {IProfession Sion; stopwatch watch = new Stopwatch (); watch. start (); for (int I = 0; I <tim E; I ++) {transfer sion = hasParameters? New Worker ("Worker-New-" + I): new Worker ();} watch. stop (); Console. write (watch. elapsedMilliseconds. toString (). padLeft (5); }/// <summary> /// create using the Activator control class /// </summary> public void CreateByActivator () {Type type = Type. getType (className); IProfession Sion; Stopwatch watch = new Stopwatch (); watch. start (); for (int I = 0; I <time; I ++) {object obj = hasParameters? Activator. createInstance (type, "worker-Activator-" + I): Activator. createInstance (type); Sion = obj as IProfession;} watch. stop (); Console. write (watch. elapsedMilliseconds. toString (). padLeft (5);} // <summary> /// create using the Assembly control class /// </summary> public void CreateByAssembly () {Assembly assembly = Assembly. getAssembly (Type. getType (className); IProfession Sion; Stopwatch watch = new Stop Watch (); watch. Start (); for (int I = 0; I <time; I ++) {object obj = hasParameters? Assembly. createInstance (className, true, BindingFlags. default, null, new object [] {"worker-Assembly-" + I}, CultureInfo. currentCulture, null): assembly. createInstance (className); Sion = obj as IProfession;} watch. stop (); Console. write (watch. elapsedMilliseconds. toString (). padLeft (5);} // <summary> // For loop, replace the For loop Code that has been written similarly multiple times in the Code /// </summary> /// <param name = "time"> Number of cycles </param> /// <param n Ame = "action"> DeleGate </param> public static void ForLoop (int time, Action <int> action) {for (int I = 0; I <time; I ++) {action (I) ;}/// <summary> /// For loop, encapsulate For loop code execution // </summary> /// <param name = "time"> Number of loops </param> /// <param name = "action"> delegate, function pointer </param> public static void ForLoop (int time, Action action) {for (int I = 0; I <time; I ++) {action () ;}}/// <summary> /// execute 10 times of 100,000 create class instances /// </summ Ary> private static void ExecuteLakh () {bool hasParameters = true; Console. writeLine ("instantiate a class ({0} parameter structure) performance comparison (unit: milliseconds)", hasParameters? "Yes": "no"); Console. write ("\ 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 ();}
The test results are as follows: