真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

直接方式,反射方式,dynamic方式性能比較-創(chuàng)新互聯(lián)

先上測試代碼

創(chuàng)新互聯(lián)公司于2013年成立,先為漳縣等服務(wù)建站,漳縣等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為漳縣企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
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("直接創(chuàng)建,賦值并訪問內(nèi)存中對象屬性:   " + sw.ElapsedTicks);
            //Console.WriteLine("直接創(chuàng)建,賦值并訪問內(nèi)存中對象屬性:   " + 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("反射創(chuàng)建,賦值并訪問內(nèi)存中對象屬性:   " + 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直接創(chuàng)建,賦值并訪問內(nèi)存中對象屬性:" + 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反射創(chuàng)建,賦值并訪問內(nèi)存中對象屬性:" + 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("直接調(diào)用內(nèi)存中對象方法:   " + 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("反射調(diào)用內(nèi)存中對象方法:   " + 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直接調(diào)用內(nèi)存中對象方法:" + 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反射調(diào)用內(nèi)存中對象方法:" + 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("使用反射加載程序集,創(chuàng)建,賦值,訪問,對象屬性:   " + 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賦值,使用反射加載程序集,創(chuàng)建,賦值,訪問,對象屬性:   " + sw.ElapsedTicks + "(" + sw.ElapsedTicks / basic + ")");
        }

    }
}

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準確進行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。


當前名稱:直接方式,反射方式,dynamic方式性能比較-創(chuàng)新互聯(lián)
URL分享:http://weahome.cn/article/cdiopd.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部