public class ReflectasmClient {
public static void main(String[] args) throws Exception {
int number = 10000000;
testJdkReflect(number);
System.out.println();
testReflectAsm(number);
}
public static void testJdkReflect(int number) throws Exception {
SomeClass someObject = new SomeClass();
Method method = SomeClass.class.getMethod("foo", String.class);
long begin = System.currentTimeMillis();
for (int j = 0; j < number; j++) {
method.invoke(someObject, "Unmi");
}
System.out.print("结束组" + (System.currentTimeMillis() - begin +" "));
}
public static void testReflectAsm(int number) {
SomeClass someObject = new SomeClass();
MethodAccess access = MethodAccess.get(SomeClass.class);
long begin = System.currentTimeMillis();
for (int j = 0; j < number; j++) {
access.invoke(someObject, "foo", "Unmi");
}
System.out.print("结束组" + (System.currentTimeMillis() - begin +" "));
}
}
结论:MethodAccess 每次缓存,性能高于JDK 原生的反射
用附中的源码包,和 asm-3.3.1.jar
public static void main(String[] args) throws Exception {
int number = 10000000;
testJdkReflect(number);
System.out.println();
testReflectAsm(number);
}
public static void testJdkReflect(int number) throws Exception {
SomeClass someObject = new SomeClass();
Method method = SomeClass.class.getMethod("foo", String.class);
long begin = System.currentTimeMillis();
for (int j = 0; j < number; j++) {
method.invoke(someObject, "Unmi");
}
System.out.print("结束组" + (System.currentTimeMillis() - begin +" "));
}
public static void testReflectAsm(int number) {
SomeClass someObject = new SomeClass();
MethodAccess access = MethodAccess.get(SomeClass.class);
long begin = System.currentTimeMillis();
for (int j = 0; j < number; j++) {
access.invoke(someObject, "foo", "Unmi");
}
System.out.print("结束组" + (System.currentTimeMillis() - begin +" "));
}
}
结论:MethodAccess 每次缓存,性能高于JDK 原生的反射
用附中的源码包,和 asm-3.3.1.jar