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

來源:互聯網
上載者:User
最近在研究一個可配置系統的架構,在代碼中大量使用了反射的方法,雖然借鑒到其他的語言,如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嘗試了一些簡單的反射,但是很顯然的,反射得消耗是非常大的。
  
  
  作者Blog:http://blog.csdn.net/Leafwiz/
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.