Fast Reflection
java refleciton api 可以让程序变得灵活,但是速度影响很大,fast Refleciton 可以解决这个问题
速度
Fast Reflction 在我的测试中所花的时间大约是javaReflection所花时间的10%-20%
用法
public static void main(String args[])
{
try
{
int N = 100000000;
FastReflect fr = new FastReflect();
Method m = TestClass.class.getMethod("setAaa",
new Class[] { int.class });
FastMethod fm = fr.getFastMethod(m);
TestClass test = new TestClass();
long start = System.currentTimeMillis();
for (int i = 0; i < N; i++)
{
m.invoke(test, 1);
}
long reflectTime = System.currentTimeMillis() - start;
start = System.currentTimeMillis();
for (int i = 0; i < N; i++)
{
fm.invoke(test, 1);
}
long fastReflectTime = System.currentTimeMillis() - start;
System.out.println("Reflect:" + reflectTime);
System.out.println("fastReflect:" + fastReflectTime);
} catch (Exception e)
{
e.printStackTrace();
}
}
public static class TestClass
{
private int aaa;
/**
* @return the aaa
*/
public int getAaa()
{
return aaa;
}
/**
* @param aaa
* the aaa to set
*/
public void setAaa(int aaa)
{
this.aaa = aaa;
}
}