zoukankan      html  css  js  c++  java
  • 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]);

     

  • 相关阅读:
    ubuntu9.04 解决关机beep声音
    『转』饯行:理想主义终结年代的七种兵器
    尼康数码单反DX Nikkor镜头介绍
    Nikkor镜头介绍
    [转]IDL中全局变量的处理
    APSC画幅
    开心时刻1
    C# 相对路径
    使用C#语言,从Excel2007中读取数据,并显示到Form中的DataGridView。
    C# 讀取Excel、xlsx文件Excel2007
  • 原文地址:https://www.cnblogs.com/revoid/p/12340188.html
Copyright © 2011-2022 走看看