CSDN博客

img jery_lee

C#中使用反射的性能分析-zz

发表于2004/10/22 22:01:00  939人阅读

分类: .NET

 最近在研究一个可配置系统的框架,在代码中大量使用了反射的方法,虽然借鉴到其他的语言,如java中反射性能都比较差,但是想到c#既然是一种强类型的语言,对于AppDomain中的类的调用应该性能不会差很多。
今天在mvp站点上看到有人说反射的性能很差,要避免使用,就写了一个简单的例子测试了一下
测试类如下:
namespace ReflectionTest.Test
{
    public class CTester
    {
        public CTester()
        {
            a = 10;
        }

        public void test1()
        {
            a = (a - 0.0001) * 1.0001;
        }
        private double a;
        public double geta() { return a; }
    }
}
首先我们对于对象的构造进行测试
测试代码如下
private void test1()
        {
            label1.Text = "";
            label3.Text = "";
            DateTime now = DateTime.Now;

            for (int i = 0; i < 1000; i++)
            {
                for (int j = 0; j < 100; j++)
                {

                    CTester aTest = new CTester();
                }
            }

            TimeSpan spand = DateTime.Now - now;
            label1.Text = "time past " + spand.ToString();
        }

        private void test2()
        {
            label2.Text = "";
            label4.Text = "";
            DateTime now = DateTime.Now;

            for (int i = 0; i < 1000; i++)
            {
                for (int j = 0; j < 100; j++)
                {
                    Type theTest = Type.GetType("ReflectionTest.Test.CTester");
                    object theobj = theTest.InvokeMember(null, BindingFlags.CreateInstance
                   , null, null, null);
                }
            }

            TimeSpan spand = DateTime.Now - now;
            label2.Text = "time past " + spand.ToString();
        }
测试结果直接调用的时间为16ms左右,而反射调用的则始终维持在5s 520ms左右,直接效率比较接近350倍。
对于这个测试,很有趣的一点是:
如果将test2中的Type theTest = Type.GetType("ReflectionTest.Test.CTester");
移到循环之外,则相应的运行时间下降为1s 332 ms , 效率相差为20倍左右。


接下来我们对成员函数调用进行了测试:

test1:

               private void button1_Click(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;

            CTester aTest = new CTester();
            for (int i = 0; i < 1000; i++)
            {
                for (int j = 0; j < 100; j++)
                {
                   
                    aTest.test1();
                }
            }

            TimeSpan spand = DateTime.Now - now;
            label1.Text = "time past " + spand.ToString();
            label3.Text = "value is now " + aTest.geta();
        }



test2:

                private void button2_Click(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;

            Type theTest = Type.GetType("ReflectionTest.Test.CTester");
            object theobj = theTest.InvokeMember(null, BindingFlags.CreateInstance
           , null, null, null);

            for (int i = 0; i < 1000; i++)
            {
                for (int j = 0; j < 100; j++)
                {
                   
                    theTest.InvokeMember("test1", BindingFlags.InvokeMethod, null, theobj, new object[0]);
                }
            }
            CTester thewar = theobj as CTester;

            TimeSpan spand = DateTime.Now - now;   
            label2.Text = "time past " + spand.ToString();
            label4.Text = "value is now " + thewar.geta();
        }


这个例子仅仅使用了invoke member进行测试
初步得到的数据如下:
test1 : 10 ms
test2:  2m 53ms

多次测试,得到的数据有轻微的波动,但是基本上的比例维持在1:250左右

对于静态方法调用
结果为5ms  - 3m 164ms

用ILDASM查看声称的IL代码,发现除了函数调用外,声称的代码基本一致,可见性能的差别是由
callvirt   instance object [mscorlib]System.Type::InvokeMember(string,
                                                                           valuetype [mscorlib]System.Reflection.BindingFlags,
                                                                           class [mscorlib]System.Reflection.Binder,
                                                                           object,
                                                                           object[])
导致的,也就是反射引起的性能损失。


虽然只用invokemember尝试了一些简单的反射,但是很显然的,反射得消耗是非常大的。



阅读全文
0 0

相关文章推荐

img
取 消
img