简单的循环(Editor)
for (int i = 0; i < 100000; i++) { CTester aTest = new CTester(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class CTester { public CTester() { a = 10; } public void test1() { a = (a - 0.0001) * 1.0001; } private double a; public double geta() { return a; } }
正常方式调用构造函数, 变量放在循环内部和外部(Editor)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
void TestWithNew() { Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 100000; i++) { CTester aTest = new CTester(); } sw.Stop(); UnityEngine.Debug.LogFormat("使用正常方式调用构造函数,变量放在循环内部, time = {0}ms", sw.ElapsedMilliseconds); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
void TestWithNew() { Stopwatch sw = new Stopwatch(); sw.Start(); CTester aTest; for (int i = 0; i < 100000; i++) { aTest = new CTester(); } sw.Stop(); UnityEngine.Debug.LogFormat("使用正常方式调用构造函数,变量放在循环内部, time = {0}ms", sw.ElapsedMilliseconds); }
使用Type.InvokeMember调用构造函数, 变量放在循环内部和外部(Editor)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
void TestWithTypeInvokeMember() { Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 100000; i++) { Type theTest = Type.GetType("CTester"); object theobj = theTest.InvokeMember(null, BindingFlags.CreateInstance, null, null, null); } sw.Stop(); UnityEngine.Debug.LogFormat("使用Type.InvokeMember调用构造函数,变量放在循环内部, time = {0}ms", sw.ElapsedMilliseconds); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
void TestWithTypeInvokeMember() { Stopwatch sw = new Stopwatch(); sw.Start(); Type theTest = Type.GetType("CTester"); for (int i = 0; i < 100000; i++) { object theobj = theTest.InvokeMember(null, BindingFlags.CreateInstance, null, null, null); } sw.Stop(); UnityEngine.Debug.LogFormat("使用Type.InvokeMember调用构造函数,变量放在循环外部, time = {0}ms", sw.ElapsedMilliseconds); }
使用ConstructInfo调用构造函数, 变量放在循环内部和外部(Editor)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
void TestWithConstructorInfo() { Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 100000; i++) { Type theTest = Type.GetType("CTester"); ConstructorInfo ci = theTest.GetConstructor(new Type[0]); System.Object obj = ci.Invoke(new System.Object[0]); } sw.Stop(); UnityEngine.Debug.LogFormat("使用ConstructorInfo调用构造函数,变量放在循环内部, time = {0}ms", sw.ElapsedMilliseconds); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
void TestWithConstructorInfo() { Stopwatch sw = new Stopwatch(); sw.Start(); Type theTest = Type.GetType("CTester"); ConstructorInfo ci = theTest.GetConstructor(new Type[0]); for (int i = 0; i < 100000; i++) { System.Object obj = ci.Invoke(new System.Object[0]); } sw.Stop(); UnityEngine.Debug.LogFormat("使用ConstructorInfo调用构造函数,变量放在循环外部, time = {0}ms", sw.ElapsedMilliseconds); }
CTester test = new CTester();
Type theTest = Type.GetType("CTester"); object theobj = theTest.InvokeMember(null, BindingFlags.CreateInstance, null, null, null);
Type theTest = Type.GetType("CTester"); ConstructorInfo ci = theTest.GetConstructor(new Type[0]); System.Object obj = ci.Invoke(new System.Object[0]);