测试分为3个方面:1. 实例化效率;2. 方法调用效率;3. 成员变量GET调用效率;4. 成员变量SET调用
测试环境:
Windows 7
Sun Java 1.6
先写好备测试用的类
import java.util.Date;
public class DemoClass{ public String firstName; private String endName; private Date birthDay; private String email; private String phone; private DemoClass parent; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getEndName() { return endName; } public void setEndName(String endName) { this.endName = endName; } public Date getBirthDay() { return birthDay; } public void setBirthDay(Date birthDay) { this.birthDay = birthDay; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public DemoClass getParent() { return parent; } public void setParent(DemoClass parent) { this.parent = parent; }}
1. 实例化效率
1) 正常调用
public class TestReflectNew {
public static void main(String[] args) throws Exception{ int i = 0; long start = System.currentTimeMillis(); while(i<1000000){ i++;// DemoClass.class.newInstance(); new DemoClass(); } long end = System.currentTimeMillis(); System.err.println((end - start) + " MillSeconds"); }}三次输出:
18 MillSeconds
19 MillSeconds
20 MillSeconds
均值:19
2) 反射调用
public class TestReflectNew {
public static void main(String[] args) throws Exception{ int i = 0; long start = System.currentTimeMillis(); while(i<1000000){ i++; DemoClass.class.newInstance();// new DemoClass(); } long end = System.currentTimeMillis(); System.err.println((end - start) + " MillSeconds"); }} 三次输出:
191 MillSeconds
193 MillSeconds192 MillSeconds
均值:192
2. 方法调用测试
1) 正常调用
public class TestReflectMethod {
public static void main(String[] args) throws Exception{ int i = 0; DemoClass dc = new DemoClass(); long start = System.currentTimeMillis(); while(i<1000000){ i++;// DemoClass.class.getMethod("getBirthDay").invoke(dc); dc.getBirthDay(); } long end = System.currentTimeMillis(); System.err.println((end - start) + " MillSeconds"); }}
三次输出:
2 MillSeconds
2 MillSeconds
2 MillSeconds
均值:2
2) 反射调用
public class TestReflectMethod {
public static void main(String[] args) throws Exception{ int i = 0; DemoClass dc = new DemoClass(); long start = System.currentTimeMillis(); while(i<1000000){ i++; DemoClass.class.getMethod("getBirthDay").invoke(dc);// dc.getBirthDay(); } long end = System.currentTimeMillis(); System.err.println((end - start) + " MillSeconds"); }}
三次输出:
1223 MillSeconds
1201 MillSeconds
1207 MillSeconds
均值1210
3. 测试成员变量GET
1) 正常调用
public class TestReflectFieldGet {
public static void main(String[] args) throws Exception{ int i = 0; DemoClass dc = new DemoClass(); String s; long start = System.currentTimeMillis(); while(i<1000000){ i++;// s = (String) DemoClass.class.getField("firstName").get(dc); s = dc.firstName; } long end = System.currentTimeMillis(); System.err.println((end - start) + " MillSeconds"); }} 三次输出:
2 MillSeconds
1 MillSeconds
3 MillSeconds
均值:2
2) 反射调用
public class TestReflectFieldGet {
public static void main(String[] args) throws Exception{ int i = 0; DemoClass dc = new DemoClass(); String s; long start = System.currentTimeMillis(); while(i<1000000){ i++; s = (String) DemoClass.class.getField("firstName").get(dc);// s = dc.firstName; } long end = System.currentTimeMillis(); System.err.println((end - start) + " MillSeconds"); }} 三次输出:
992 MillSeconds
1000 MillSeconds
1005 MillSeconds
均值:999
4. 测试成员变量SET
1) 正常调用
public class TestReflectFieldSet {
public static void main(String[] args) throws Exception { int i = 0; DemoClass dc = new DemoClass(); String s = null; long start = System.currentTimeMillis(); while(i<1000000){ i++; dc.firstName = s;// DemoClass.class.getField("firstName").set(dc, s); } long end = System.currentTimeMillis(); System.err.println((end - start) + " MillSeconds"); }}
三次输出:
1 MillSeconds
2 MillSeconds
2 MillSeconds
均值:1.7
2) 反射调用
public class TestReflectFieldSet {
public static void main(String[] args) throws Exception { int i = 0; DemoClass dc = new DemoClass(); String s = null; long start = System.currentTimeMillis(); while(i<1000000){ i++;// dc.firstName = s; DemoClass.class.getField("firstName").set(dc, s); } long end = System.currentTimeMillis(); System.err.println((end - start) + " MillSeconds"); }} 三次输出:
998 MillSeconds
1021 MillSeconds
1007 MillSeconds
均值:1009
来个统计
反射平均时间 非反射平均时间 比值 实例化 192 19 10 方法调用 1210 2 605 成员Get 999 2 500 成员Set 1009 1.7 593