189 8069 5689

直接方式,反射方式,dynamic方式性能比较

先上测试代码 

成都网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设等网站项目制作,到程序开发,运营维护。创新互联建站于2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联建站

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 + ")");
        }

    }
}


直接方式,反射方式,dynamic方式性能比较

直接方式,反射方式,dynamic方式性能比较

直接方式,反射方式,dynamic方式性能比较

直接方式,反射方式,dynamic方式性能比较

分别循环1000,10000,50000,100000次

所得到的数据显示,直接创建赋值是快速的方法。

反射创建所需要的时间比依次是9x,15x,15x,15x

对于两种dynamic,

所需的时间比依次是36,8,3,2和7,4,3,3。说明了在反复调用次数少的情况下,dynamic方式并不优于反射,但是在反复调用次数多的情况下,使用dynamic方式优于直接使用反射。两种dynamic方式在调用次数多的情况下,差距也不是很多

对于方法的调用,发现,时间比都差不多。反射稍慢一点。dynamic方式略占优势。

对于加载assembly的方式,绝对是最慢的,通常要在100-150倍左右。
 


此处我想强调的是,虽然速度差别很大,但是通常的代码运行很少有循环1000次10000次的。即便有,其绝对值还是很小的。相对网络延迟开销,反射所花去的开销可以忽略不计。因此,因为性能问题拒绝反射是愚蠢的。使用dynamic类型,相对反射来说,代码可以写的更优雅。

以上数据,仅供参考。

附件:http://down.51cto.com/data/2361091

当前题目:直接方式,反射方式,dynamic方式性能比较
本文来源:http://jkwzsj.com/article/gegipo.html

其他资讯