C#中使用反射的效能分析zz
最後更新:2018-12-07
來源:互聯網
上載者: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/