Unity下Reflection相关测试记录

简单的循环(Editor)

for (int i = 0; i < 100000; i++) {
    CTester aTest = new CTester();
}

 

public class CTester {
    public CTester() {
        a = 10;
    }

    public void test1() {
        a = (a - 0.0001) * 1.0001;
    }

    private double a;

    public double geta() { return a; }
}
CTester

 

正常方式调用构造函数, 变量放在循环内部和外部(Editor)

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);
}
使用new调用构造函数,变量放在循环内部  GCAlloc = 2.3MB

 

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);
}
使用new调用构造函数,变量放在循环外部  GCAlloc = 2.3MB

 

使用Type.InvokeMember调用构造函数, 变量放在循环内部和外部(Editor)

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);
}
使用Type.InvokeMember调用构造函数, 变量放在循环内部  GCAlloc = 57.2MB

 

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);
}
使用Type.InvokeMember调用构造函数, 变量放在循环外部  GCAlloc = 57.2MB

 

使用ConstructInfo调用构造函数, 变量放在循环内部和外部(Editor)

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);
}
使用ConstructorInfo调用构造函数,变量放在循环内部  GCAlloc = 21.4MB

 

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);
}
使用ConstructorInfo调用构造函数,变量放在循环外部  GCAlloc = 8.4MB

 

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]);

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2020-02-21 09:15  void87  阅读(241)  评论(0编辑  收藏  举报

导航