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

     

  • 相关阅读:
    生成水印图片
    Java 生成图片-字体杂色去除
    java比较两个日期大小
    JS--添加option
    使用JS刷新showModalDialog窗口
    日期格式转换(String->Date)
    根据当前日期及出生日期,计算当前年龄(function)
    PHP的错误和异常处理
    IntelliJ IDEA15,PhpStorm10,WebStorm11激活破解
    apache相关配置
  • 原文地址:https://www.cnblogs.com/revoid/p/12340188.html
Copyright © 2011-2022 走看看