• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • C#中使用反射的使用實現和性能分析

    發表于:2007-05-26來源:作者:點擊數: 標簽:
    最近在研究一個可配置系統的框架,在代碼中大量使用了反射的方法,雖然借鑒到其他的語言,如 Java 中反射 性能 都比較差,但是想到C#既然是一種強類型的語言,對于AppDomain中的類的調用應該性能不會差很多。 今天在mvp站點上看到有人說反射的性能很差,要避

    最近在研究一個可配置系統的框架,在代碼中大量使用了反射的方法,雖然借鑒到其他的語言,如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嘗試了一些簡單的反射,但是很顯然的,反射得消耗是非常大的。

    原文轉自:http://www.kjueaiud.com

    評論列表(網友評論僅供網友表達個人看法,并不表明本站同意其觀點或證實其描述)
    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>