对对象类型和调用方法属性进行存储以提升反射性能
生活随笔
收集整理的這篇文章主要介紹了
对对象类型和调用方法属性进行存储以提升反射性能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
反射的性能差是一個公認的事實.而最耗性能的還是根據程序集獲取要調用的對象,而在對象里搜索要調用的方法所耗性能到不不是很多,如果對象里的方法不是特別的多,而去可以指定相關參數提高搜索的效率,比如BindingFlags,Binder.
如果將對象的類型和方法屬性進行一下存儲,性能方法應該會得到改觀.
簡單的做了下測試:
??? class?Program
????{
????????static?IDictionary<string?,?Type>?typeList?=?new?Dictionary<string?,?Type>();
????????static?IDictionary<string?,?MethodInfo>?methodList?=?new?Dictionary<string?,?MethodInfo>();
????????static?void?Main?(?string[]?args?)
????????{
????????????int?iBegin?=?Environment.TickCount;
????????????for?(?int?i?=?0?;?i?<?1000?;?i++?)?
????????????{
????????????????Test1(?"Test.TestClass,Test"?);
????????????????Test1(?"Test1.Class1,Test1"?);??
????????????}
????????????Console.WriteLine(?"普通:{0}"?,?Environment.TickCount?-?iBegin?);
????????????
????????????GC.Collect();
????????????iBegin?=?Environment.TickCount;
????????????for?(?int?i?=?0?;?i?<?1000?;?i++?)
????????????{
????????????????Test2(?"Test.TestClass,Test"?);?????
????????????????Test2(?"Test1.Class1,Test1"?);
????????????}
????????????Console.WriteLine(?"存儲Type:{0}"?,?Environment.TickCount?-?iBegin?);
????????????GC.Collect();
????????????iBegin?=?Environment.TickCount;
????????????for?(?int?i?=?0?;?i?<?1000?;?i++?)
????????????{
????????????????Test3(?"Test.TestClass,Test"?);
????????????????Test3(?"Test1.Class1,Test1"?);
????????????}
????????????Console.WriteLine(?"存儲MethodInfo:{0}"?,?Environment.TickCount?-?iBegin?);
????????????GC.Collect();
????????????Console.ReadLine();
????????}
????????static?void?Test1?(?string?typeName?)
????????{
????????????Type?type?=?Type.GetType(?typeName?);
????????????object?instance?=?Activator.CreateInstance(?type?,?1?);
????????????MethodInfo?methodInfo?=?type.GetMethod(?"GetValue"?);
????????????methodInfo.Invoke(?instance?,?null?);
????????????
????????????instance?=?Activator.CreateInstance(?type?,?null?);
????????????methodInfo?=?type.GetMethod(?"Add"?,?new?Type[]?{?typeof(?int?)?,?typeof(?int?)?}?);
????????????methodInfo.Invoke(?instance?,?new?object[]?{?1?,?2?}?);
????????}
????????//存儲Type
????????static?void?Test2?(?string?typeName?)
????????{
????????????Type?type?=?GetType(?typeName?);
????????????object?instance?=?Activator.CreateInstance(?type?,?1?);
????????????MethodInfo?methodInfo?=?type.GetMethod(?"GetValue"?);
????????????methodInfo.Invoke(?instance?,?null?);
????????????instance?=?Activator.CreateInstance(?type?,?null?);
????????????methodInfo?=?type.GetMethod(?"Add"?,?new?Type[]?{?typeof(?int?)?,?typeof(?int?)?}?);
????????????methodInfo.Invoke(?instance?,?new?object[]?{?1?,?2?}?);
????????}
????????//存儲MethodInfo
????????static?void?Test3?(?string?typeName?)
????????{
????????????Type?type?=?GetType(?typeName?);
????????????object?instance?=?Activator.CreateInstance(?type?,?1?);
????????????MethodInfo?methodInfo?=?GetMethod(?typeName?,?"GetValue"?,?type?,?new?Type[0]?);
????????????methodInfo.Invoke(?instance?,?null?);
????????????instance?=?Activator.CreateInstance(?type?,?null?);
????????????methodInfo?=?GetMethod(?typeName?,?"Add"?,?type?,?new?Type[]?{?typeof(?int?)?,?typeof(?int?)?}?);
????????????methodInfo.Invoke(?instance?,?new?object[]?{?1?,?2?}?);
????????}
????????static?Type?GetType?(?string?typeName?)
????????{
????????????Type?type?=?null;
????????????if?(?!typeList.ContainsKey(?typeName?)?)
????????????{
????????????????type?=?Type.GetType(?typeName?);
????????????????typeList.Add(?typeName?,?type?);
????????????}
????????????else
????????????????type?=?typeList[typeName];
????????????return?type;
????????}
????????static?MethodInfo?GetMethod?(?string?typeName?,?string?methodName?,?Type?type?,?params?Type[]?types?)
????????{
????????????MethodInfo?methodInfo?=?null;
????????????if?(?!methodList.ContainsKey(?typeName?+?methodName?)?)
????????????{
????????????????methodInfo?=?type.GetMethod(?methodName?,?types?);
????????????????methodList.Add(?typeName?+?methodName?,?methodInfo?);
????????????}
????????????else
????????????????methodInfo?=?methodList[typeName?+?methodName];
????????????return?methodInfo;
????????}????????
????} 其中 Test.TestClass 類和 Test1.Class1 都有一個無參構造函數和一個int類型的參數及2個方法:
int?val?=?0;
public?Class1?(?int?val?)
{
?????this.val?=?val;
}
public?string?GetValue?()
{
?????return?string.Format(?"the?value?is?{0}"?,?val?);
}
public?int?Add?(?int?a?,?int?b?)
{
?????return?a?+?b;
} 為了測試,上面2個類里都加了幾個方法(只是定義,并沒有具體的實現代碼).
測試了下不進行任何處理,時間都在130左右波動,對Type進行存儲,時間在32左右,基本上沒什么波幅,而對Type和MethodInfo進行存儲時間維持在31.
看來這樣處理下對性能的改善還是起到了作用.
如果將對象的類型和方法屬性進行一下存儲,性能方法應該會得到改觀.
簡單的做了下測試:
??? class?Program
????{
????????static?IDictionary<string?,?Type>?typeList?=?new?Dictionary<string?,?Type>();
????????static?IDictionary<string?,?MethodInfo>?methodList?=?new?Dictionary<string?,?MethodInfo>();
????????static?void?Main?(?string[]?args?)
????????{
????????????int?iBegin?=?Environment.TickCount;
????????????for?(?int?i?=?0?;?i?<?1000?;?i++?)?
????????????{
????????????????Test1(?"Test.TestClass,Test"?);
????????????????Test1(?"Test1.Class1,Test1"?);??
????????????}
????????????Console.WriteLine(?"普通:{0}"?,?Environment.TickCount?-?iBegin?);
????????????
????????????GC.Collect();
????????????iBegin?=?Environment.TickCount;
????????????for?(?int?i?=?0?;?i?<?1000?;?i++?)
????????????{
????????????????Test2(?"Test.TestClass,Test"?);?????
????????????????Test2(?"Test1.Class1,Test1"?);
????????????}
????????????Console.WriteLine(?"存儲Type:{0}"?,?Environment.TickCount?-?iBegin?);
????????????GC.Collect();
????????????iBegin?=?Environment.TickCount;
????????????for?(?int?i?=?0?;?i?<?1000?;?i++?)
????????????{
????????????????Test3(?"Test.TestClass,Test"?);
????????????????Test3(?"Test1.Class1,Test1"?);
????????????}
????????????Console.WriteLine(?"存儲MethodInfo:{0}"?,?Environment.TickCount?-?iBegin?);
????????????GC.Collect();
????????????Console.ReadLine();
????????}
????????static?void?Test1?(?string?typeName?)
????????{
????????????Type?type?=?Type.GetType(?typeName?);
????????????object?instance?=?Activator.CreateInstance(?type?,?1?);
????????????MethodInfo?methodInfo?=?type.GetMethod(?"GetValue"?);
????????????methodInfo.Invoke(?instance?,?null?);
????????????
????????????instance?=?Activator.CreateInstance(?type?,?null?);
????????????methodInfo?=?type.GetMethod(?"Add"?,?new?Type[]?{?typeof(?int?)?,?typeof(?int?)?}?);
????????????methodInfo.Invoke(?instance?,?new?object[]?{?1?,?2?}?);
????????}
????????//存儲Type
????????static?void?Test2?(?string?typeName?)
????????{
????????????Type?type?=?GetType(?typeName?);
????????????object?instance?=?Activator.CreateInstance(?type?,?1?);
????????????MethodInfo?methodInfo?=?type.GetMethod(?"GetValue"?);
????????????methodInfo.Invoke(?instance?,?null?);
????????????instance?=?Activator.CreateInstance(?type?,?null?);
????????????methodInfo?=?type.GetMethod(?"Add"?,?new?Type[]?{?typeof(?int?)?,?typeof(?int?)?}?);
????????????methodInfo.Invoke(?instance?,?new?object[]?{?1?,?2?}?);
????????}
????????//存儲MethodInfo
????????static?void?Test3?(?string?typeName?)
????????{
????????????Type?type?=?GetType(?typeName?);
????????????object?instance?=?Activator.CreateInstance(?type?,?1?);
????????????MethodInfo?methodInfo?=?GetMethod(?typeName?,?"GetValue"?,?type?,?new?Type[0]?);
????????????methodInfo.Invoke(?instance?,?null?);
????????????instance?=?Activator.CreateInstance(?type?,?null?);
????????????methodInfo?=?GetMethod(?typeName?,?"Add"?,?type?,?new?Type[]?{?typeof(?int?)?,?typeof(?int?)?}?);
????????????methodInfo.Invoke(?instance?,?new?object[]?{?1?,?2?}?);
????????}
????????static?Type?GetType?(?string?typeName?)
????????{
????????????Type?type?=?null;
????????????if?(?!typeList.ContainsKey(?typeName?)?)
????????????{
????????????????type?=?Type.GetType(?typeName?);
????????????????typeList.Add(?typeName?,?type?);
????????????}
????????????else
????????????????type?=?typeList[typeName];
????????????return?type;
????????}
????????static?MethodInfo?GetMethod?(?string?typeName?,?string?methodName?,?Type?type?,?params?Type[]?types?)
????????{
????????????MethodInfo?methodInfo?=?null;
????????????if?(?!methodList.ContainsKey(?typeName?+?methodName?)?)
????????????{
????????????????methodInfo?=?type.GetMethod(?methodName?,?types?);
????????????????methodList.Add(?typeName?+?methodName?,?methodInfo?);
????????????}
????????????else
????????????????methodInfo?=?methodList[typeName?+?methodName];
????????????return?methodInfo;
????????}????????
????} 其中 Test.TestClass 類和 Test1.Class1 都有一個無參構造函數和一個int類型的參數及2個方法:
int?val?=?0;
public?Class1?(?int?val?)
{
?????this.val?=?val;
}
public?string?GetValue?()
{
?????return?string.Format(?"the?value?is?{0}"?,?val?);
}
public?int?Add?(?int?a?,?int?b?)
{
?????return?a?+?b;
} 為了測試,上面2個類里都加了幾個方法(只是定義,并沒有具體的實現代碼).
測試了下不進行任何處理,時間都在130左右波動,對Type進行存儲,時間在32左右,基本上沒什么波幅,而對Type和MethodInfo進行存儲時間維持在31.
看來這樣處理下對性能的改善還是起到了作用.
轉載于:https://www.cnblogs.com/doll-net/archive/2008/04/25/Reflection.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的对对象类型和调用方法属性进行存储以提升反射性能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我的第一份外包经历及所得
- 下一篇: 细节:关于异步调用的解决方案