反射是一个重要的知识点,我们来测试一下循环十亿次,普通方法,反射方法和关闭检测的反射方法运行时间有什么不同
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
//测试反射性能
public class Test02 {
//普通方法
public static void test01(){
User user = new User();
long starttime = System.currentTimeMillis();
for (int i = 0; i < 1000000000; i++) {
user.getName();
}
long endtime = System.currentTimeMillis();
System.out.println("普通方法:"+(endtime - starttime)+" ms");
}
//反射方法
public static void test02() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
User user = new User();
Class c1 = user.getClass();
Method getName = c1.getDeclaredMethod("getName", null);
long starttime = System.currentTimeMillis();
for (int i = 0; i < 100