本篇文章給大家分享的是有關(guān)Java反射獲取實(shí)例的速度對(duì)比,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
public static void main(String[] args) { try { int MAX = 100000; for (int count = 0; count < 50; count++) { System.out.println("====第" + count+"次"); long s1 = System.currentTimeMillis(); for (int i = 0; i < MAX; i++) { Person o = (Person)Class.forName("com.qingtai.domin.Person").newInstance(); } long e1 = System.currentTimeMillis(); System.out.println("1_duration:" + (e1 - s1)); long s2 = System.currentTimeMillis(); Class clazz = Class.forName("com.qingtai.domin.Person"); for (int i = 0; i < MAX; i++) { Person person = (Person) clazz.newInstance(); } long e2 = System.currentTimeMillis(); System.out.println("2_duration:" + (e2 - s2)); } } catch (Exception e) { e.printStackTrace(); } }