测试Java反射效率

测试分为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 MillSeconds
192 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

 

来个统计

反射平均时间非反射平均时间比值
实例化1921910
方法调用12102605
成员Get9992500
成员Set10091.7593

 

 

以上测试的方式如果有不合理的地方请eye友们指点,看看测试的方法是否得当,是否全面

 

本人新博客:tuoxie.me

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值