对对象类型和调用方法属性进行存储以提升反射性能

反射的性能差是一个公认的事实.而最耗性能的还是根据程序集获取要调用的对象,而在对象里搜索要调用的方法所耗性能到不不是很多,如果对象里的方法不是特别的多,而去可以指定相关参数提高搜索的效率,比如BindingFlags,Binder.
如果将对象的类型和方法属性进行一下存储,性能方法应该会得到改观.
简单的做了下测试:
classProgram
{
staticIDictionary<string,Type>typeList=newDictionary<string,Type>();

staticIDictionary<string,MethodInfo>methodList=newDictionary<string,MethodInfo>();

staticvoidMain(string[]args)
{
intiBegin=Environment.TickCount;
for(inti=0;i<1000;i++)
{
Test1(
"Test.TestClass,Test");
Test1(
"Test1.Class1,Test1");
}
Console.WriteLine(
"普通:{0}",Environment.TickCount-iBegin);

GC.Collect();
iBegin
=Environment.TickCount;
for(inti=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(inti=0;i<1000;i++)
{
Test3(
"Test.TestClass,Test");
Test3(
"Test1.Class1,Test1");
}
Console.WriteLine(
"存储MethodInfo:{0}",Environment.TickCount-iBegin);
GC.Collect();

Console.ReadLine();
}

staticvoidTest1(stringtypeName)
{
Typetype
=Type.GetType(typeName);
objectinstance=Activator.CreateInstance(type,1);
MethodInfomethodInfo
=type.GetMethod("GetValue");
methodInfo.Invoke(instance,
null);

instance
=Activator.CreateInstance(type,null);
methodInfo
=type.GetMethod("Add",newType[]{typeof(int),typeof(int)});
methodInfo.Invoke(instance,
newobject[]{1,2});
}

//存储Type
staticvoidTest2(stringtypeName)
{
Typetype
=GetType(typeName);

objectinstance=Activator.CreateInstance(type,1);
MethodInfomethodInfo
=type.GetMethod("GetValue");
methodInfo.Invoke(instance,
null);

instance
=Activator.CreateInstance(type,null);
methodInfo
=type.GetMethod("Add",newType[]{typeof(int),typeof(int)});
methodInfo.Invoke(instance,
newobject[]{1,2});
}

//存储MethodInfo
staticvoidTest3(stringtypeName)
{
Typetype
=GetType(typeName);
objectinstance=Activator.CreateInstance(type,1);
MethodInfomethodInfo
=GetMethod(typeName,"GetValue",type,newType[0]);
methodInfo.Invoke(instance,
null);

instance
=Activator.CreateInstance(type,null);
methodInfo
=GetMethod(typeName,"Add",type,newType[]{typeof(int),typeof(int)});
methodInfo.Invoke(instance,
newobject[]{1,2});
}

staticTypeGetType(stringtypeName)
{
Typetype
=null;
if(!typeList.ContainsKey(typeName))
{
type
=Type.GetType(typeName);
typeList.Add(typeName,type);
}
else
type
=typeList[typeName];
returntype;
}

staticMethodInfoGetMethod(stringtypeName,stringmethodName,Typetype,paramsType[]types)
{
MethodInfomethodInfo
=null;
if(!methodList.ContainsKey(typeName+methodName))
{
methodInfo
=type.GetMethod(methodName,types);
methodList.Add(typeName
+methodName,methodInfo);
}
else
methodInfo
=methodList[typeName+methodName];
returnmethodInfo;
}
}
其中 Test.TestClass 类和 Test1.Class1 都有一个无参构造函数和一个int类型的参数及2个方法:
intval=0;
publicClass1(intval)
{
this.val=val;
}
publicstringGetValue()
{
returnstring.Format("thevalueis{0}",val);
}
publicintAdd(inta,intb)
{
returna+b;
}
为了测试,上面2个类里都加了几个方法(只是定义,并没有具体的实现代码).
测试了下不进行任何处理,时间都在130左右波动,对Type进行存储,时间在32左右,基本上没什么波幅,而对Type和MethodInfo进行存储时间维持在31.
看来这样处理下对性能的改善还是起到了作用.

阅读更多

没有更多推荐了,返回首页