您好,登錄后才能下訂單哦!
先上測試代碼
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("直接創(chuàng)建,賦值并訪問內(nèi)存中對象屬性: " + sw.ElapsedTicks); //Console.WriteLine("直接創(chuàng)建,賦值并訪問內(nèi)存中對象屬性: " + 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("反射創(chuàng)建,賦值并訪問內(nèi)存中對象屬性: " + 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直接創(chuàng)建,賦值并訪問內(nèi)存中對象屬性:" + 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反射創(chuàng)建,賦值并訪問內(nèi)存中對象屬性:" + 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("直接調(diào)用內(nèi)存中對象方法: " + 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("反射調(diào)用內(nèi)存中對象方法: " + 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直接調(diào)用內(nèi)存中對象方法:" + 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反射調(diào)用內(nèi)存中對象方法:" + 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("使用反射加載程序集,創(chuàng)建,賦值,訪問,對象屬性: " + 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賦值,使用反射加載程序集,創(chuàng)建,賦值,訪問,對象屬性: " + sw.ElapsedTicks + "(" + sw.ElapsedTicks / basic + ")"); } } }
分別循環(huán)1000,10000,50000,100000次
所得到的數(shù)據(jù)顯示,直接創(chuàng)建賦值是快速的方法。
反射創(chuàng)建所需要的時間比依次是9x,15x,15x,15x
對于兩種dynamic,
所需的時間比依次是36,8,3,2和7,4,3,3。說明了在反復(fù)調(diào)用次數(shù)少的情況下,dynamic方式并不優(yōu)于反射,但是在反復(fù)調(diào)用次數(shù)多的情況下,使用dynamic方式優(yōu)于直接使用反射。兩種dynamic方式在調(diào)用次數(shù)多的情況下,差距也不是很多
對于方法的調(diào)用,發(fā)現(xiàn),時間比都差不多。反射稍慢一點。dynamic方式略占優(yōu)勢。
對于加載assembly的方式,絕對是最慢的,通常要在100-150倍左右。
此處我想強調(diào)的是,雖然速度差別很大,但是通常的代碼運行很少有循環(huán)1000次10000次的。即便有,其絕對值還是很小的。相對網(wǎng)絡(luò)延遲開銷,反射所花去的開銷可以忽略不計。因此,因為性能問題拒絕反射是愚蠢的。使用dynamic類型,相對反射來說,代碼可以寫的更優(yōu)雅。
以上數(shù)據(jù),僅供參考。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。