• 性能报告——反射创建对象


    代码
            public void testperformance()
            {
                
    string assembly = "Pixysoft.Framework.Noebe.Orm";
                
    string type = "Pixysoft.Framework.Noebe.Orm.testdriven.Pojo";

                CodeTimer.Initialize();

                CodeTimer.Time(
    "reflection"100000delegate
                {
                    Type _type 
    = Assembly.Load(assembly).GetType(type);
                    Object obj 
    = Activator.CreateInstance(_type);
                });

                CodeTimer.Time(
    "direct"100000delegate
                {
                    Object obj 
    = new Pojo();
                });

                CodeTimer.Time(
    "dynamic"100000delegate
                {
                    Type _type 
    = Assembly.Load(assembly).GetType(type);
                    Object obj 
    = ReflectionManager.CreateDynamicType(_type).GetDefaultConstructor().Invoke();
                });
            }

    ------ Test started: Assembly: Pixysoft.Framework.Noebe.Orm.dll ------


    reflection

    Time Elapsed: 2,422ms

    CPU time: 2,421,875,000ns

    Gen 0: 19

    Gen 1: 0

    Gen 2: 0


    direct

    Time Elapsed: 1ms

    CPU time: 0ns

    Gen 0: 1

    Gen 1: 0

    Gen 2: 0


    dynamic

    Time Elapsed: 2,726ms

    CPU time: 2,718,750,000ns

    Gen 0: 91

    Gen 1: 0

    Gen 2: 0



    1 passed, 0 failed, 0 skipped, took 5.41 seconds (Ad hoc).



    结论:

    1. 反射创建对象的开销基本上可以忽略了。

    2. 自己使用动态等方法创建对象并没有很大提高性能。

    3. 反射创建对象的唯一难点在 参考插件里面出现的问题:加载搜索+多dll版本内存重复。 

  • 相关阅读:
    HttpModule,HttpHandler,HttpHandlerFactory
    IHttpModule实现URL重写
    HttpModule与HttpHandler详解
    IHttpModule接口
    VS中新建网站和新建项目web应用程序的区别?(实际应用总结一点)
    slf4j简介
    SQL条件表达式
    Windows命令查看文件MD5
    Final对象
    Seasar2框架:AOP
  • 原文地址:https://www.cnblogs.com/zc22/p/1786185.html
Copyright © 2020-2023  润新知