1、首先我们对二者的执行速度做一个比较:
调用9000万次累加,观察二者花费的时间
2、然后我们给出调优的方法——关闭访问检查setAccessible(true)
观察调优后花费的时间

代码:

package reflect_optimize;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;/** * 对比使用传统方式调用方法和使用反射调用方法的速度性能差异 */public class SpeedCompare {public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {m1();m2();m3();}//1、使用传统方法public static void m1(){Dog dog = new Dog();long start = System.currentTimeMillis();for (int i = 0; i < 90000000; i++) { //调用9000万次dog.say();}long end = System.currentTimeMillis();System.out.println("1、传统方法用时:" + (end - start));}//2、使用反射public static void m2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {Class cls = Class.forName("reflect_optimize.Dog");Object o = cls.newInstance();Method method = cls.getMethod("say");long start = System.currentTimeMillis();for (int i = 0; i  关闭访问检查public static void m3() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {Class cls = Class.forName("reflect_optimize.Dog");Object o = cls.newInstance();Method method = cls.getMethod("say");method.setAccessible(true); //这一句就是优化,关闭了访问检查,同样Method、构造器也都有这个方法long start = System.currentTimeMillis();for (int i = 0; i < 90000000; i++) {method.invoke(o);}long end = System.currentTimeMillis();System.out.println("3、优化后的反射用时:" + (end - start));}}class Dog {private int count = 0;public void say() {count++;}}

执行结果:(单位ms)