我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

直接方式,反射方式,dynamic方式性能比较-创新互联

先上测试代码

创新互联公司于2013年成立,先为漳县等服务建站,漳县等地企业,进行企业商务咨询服务。为漳县企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
using System.Text;
namespace Model
{
    public partial class Class1
    {
        public Class1()
        { }

        private string _s = "";
        private int _i = 0;
        private decimal _d = 0;
        private StringBuilder _sb = new StringBuilder();
        public string S
        {
            set { _s = value; }
            get { return _s; }
        }

        public int I
        {
            set { _i = value; }
            get { return _i; }
        }

        public decimal D
        {
            set { _d = value; }
            get { return _d; }
        }

        public StringBuilder SB
        {
            set { _sb = value; }
            get { return _sb; }
        }

        public int func()
        {
            int s = 0;
            for (int i = 0; i < 1000; i++)
            {
                s = s + i;
            }
            return s;
        }
    }
}

///////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Reflection;
using System.Diagnostics;

namespace testReflect
{
    class Program
    {
        static void Main(string[] args)
        {
            const int count = 100000;
            Console.WriteLine("count="+count);

            Stopwatch sw = new Stopwatch();
            StringBuilder test = new StringBuilder();
            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Class1 m = new Class1();
                m.D = 100;
                m.I = 200;
                m.S = "string";
                m.SB = new StringBuilder("StringBuilder");

                test.Append(m.D);
                test.Append(m.I);
                test.Append(m.S);
                test.Append(m.SB);
            }
            sw.Stop();
            Console.WriteLine("直接创建,赋值并访问内存中对象属性:   " + sw.ElapsedTicks);
            //Console.WriteLine("直接创建,赋值并访问内存中对象属性:   " + sw.ElapsedTicks);
            long basic = sw.ElapsedTicks;

            sw.Restart();
            test = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                var t = typeof(Class1);
                ConstructorInfo ct = t.GetConstructor(new Type[] { });
                var m = ct.Invoke(null);
                PropertyInfo pd = t.GetProperty("D");
                pd.SetValue(m, 100m, null);
                PropertyInfo pi = t.GetProperty("I");
                pi.SetValue(m, 200, null);
                PropertyInfo ps = t.GetProperty("S");
                ps.SetValue(m, "string", null);
                PropertyInfo psb = t.GetProperty("StringBuilder");

                PropertyInfo[] props = m.GetType().GetProperties();
                foreach (var p in props)
                {
                    test.Append(p.GetValue(m, null));
                }
            }
            sw.Stop();
            Console.WriteLine("反射创建,赋值并访问内存中对象属性:   " + sw.ElapsedTicks + "(" + sw.ElapsedTicks / basic + ")");


            test = new StringBuilder();
            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                dynamic dm = new Class1();
                dm.D = 100;
                dm.I = 200;
                dm.S = "string";
                dm.SB = new StringBuilder("StringBuilder");

                test.Append(dm.D);
                test.Append(dm.I);
                test.Append(dm.S);
                test.Append(dm.SB);
            }
            sw.Stop();
            Console.WriteLine("dynamic直接创建,赋值并访问内存中对象属性:" + sw.ElapsedTicks + "(" + sw.ElapsedTicks / basic + ")");

            test = new StringBuilder();
            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                var t = typeof(Class1);
                ConstructorInfo ct = t.GetConstructor(new Type[] { });
                dynamic dm = ct.Invoke(null);
                dm.D = 100;
                dm.I = 200;
                dm.S = "string";
                dm.SB = new StringBuilder("StringBuilder");

                test.Append(dm.D);
                test.Append(dm.I);
                test.Append(dm.S);
                test.Append(dm.SB);
            }
            sw.Stop();
            Console.WriteLine("dynamic反射创建,赋值并访问内存中对象属性:" + sw.ElapsedTicks + "(" + sw.ElapsedTicks / basic + ")");

            test = new StringBuilder();
            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                Class1 m = new Class1();
                test.Append(m.func());
            }
            sw.Stop();
            Console.WriteLine("直接调用内存中对象方法:   " + sw.ElapsedTicks + "(" + sw.ElapsedTicks / basic + ")");


            sw.Restart();
            test = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                var t = typeof(Class1);
                MethodInfo meth = t.GetMethod("func");
                ConstructorInfo ct = t.GetConstructor(new Type[] { });
                test.Append(meth.Invoke(ct.Invoke(null), null));
            }
            sw.Stop();
            Console.WriteLine("反射调用内存中对象方法:   " + sw.ElapsedTicks + "(" + sw.ElapsedTicks / basic + ")");


            test = new StringBuilder();
            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                dynamic dm = new Class1();
                test.Append(dm.func());
            }
            sw.Stop();
            Console.WriteLine("dynamic直接调用内存中对象方法:" + sw.ElapsedTicks + "(" + sw.ElapsedTicks / basic + ")");

            test = new StringBuilder();
            sw.Restart();
            for (int i = 0; i < count; i++)
            {
                var t = typeof(Class1);
                ConstructorInfo ct = t.GetConstructor(new Type[] { });
                dynamic dm = ct.Invoke(null);
                test.Append(dm.func());
            }
            sw.Stop();
            Console.WriteLine("dynamic反射调用内存中对象方法:" + sw.ElapsedTicks + "(" + sw.ElapsedTicks / basic + ")");



            Console.WriteLine("###############################");

            sw.Restart();
            test = new StringBuilder();
            for (int i = 0; i < count; i++)
            {

                Assembly a = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + @"\Model.dll");
                var t = a.GetType("Model.Class1");
                ConstructorInfo ct = t.GetConstructor(new Type[] { });
                var rm = ct.Invoke(null);
                PropertyInfo pd = t.GetProperty("D");
                pd.SetValue(rm, 100m, null);
                PropertyInfo pi = t.GetProperty("I");
                pi.SetValue(rm, 200, null);
                PropertyInfo ps = t.GetProperty("S");
                ps.SetValue(rm, "string", null);
                PropertyInfo psb = t.GetProperty("SB");
                psb.SetValue(rm, new StringBuilder("StringBuilder"), null);

                PropertyInfo[] props = t.GetProperties();
                foreach (var p in props)
                {
                    test.Append(p.GetValue(rm, null));
                }
            }
            sw.Stop();
            Console.WriteLine("使用反射加载程序集,创建,赋值,访问,对象属性:   " + sw.ElapsedTicks + "(" + sw.ElapsedTicks / basic + ")");



            sw.Restart();
            test = new StringBuilder();
            for (int i = 0; i < count; i++)
            {

                Assembly a = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + @"\Model.dll");
                var t = a.GetType("Model.Class1");
                ConstructorInfo ct = t.GetConstructor(new Type[] { });
                dynamic dm = ct.Invoke(null);
                dm.D = 100m;
                dm.I = 200;
                dm.S = "string";
                dm.SB = new StringBuilder("StringBuilder");

                test.Append(dm.D);
                test.Append(dm.I);
                test.Append(dm.S);
                test.Append(dm.SB);

            }
            sw.Stop();
            Console.WriteLine("dynamic赋值,使用反射加载程序集,创建,赋值,访问,对象属性:   " + sw.ElapsedTicks + "(" + sw.ElapsedTicks / basic + ")");
        }

    }
}

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


文章名称:直接方式,反射方式,dynamic方式性能比较-创新互联
网页链接:http://shouzuofang.com/article/cdiopd.html

其他资讯