关于在Java使用反射对属性取值和赋值的性能问题。

最近在使用因为受不了Hibernate的各种问题,自己用jdbc在做一个简单的Orm框架 。既然涉及到Orm框架,肯定少不了使用反射对Bean进行赋值。

我最初是参考struts2的IOC,使用setXXX和getXXX进行赋值取值。但是后来发现,直接对属性赋值的性能更加好。

下面赋上我的测试代码,我对每行关键代码做了时间记录,以便清楚地看出哪些代码性能损耗高:

打开Accessible,对属性赋值:

/**
	 * 直接打开Accessible,对属性进行赋值
	 * @throws Exception
	 */
	@Test
	public void testFieldSet() throws Exception {
		Class<User> clazz = User.class;
		Field idField = clazz.getDeclaredField("id");
		Object obj = clazz.newInstance();
		long start = System.currentTimeMillis();
		long a = 0,b = 0,c = 0;
		for(int i=0;i<10000000;i++){
			long aa = System.currentTimeMillis();
			idField.setAccessible(true);
			long bb = System.currentTimeMillis();
			idField.set(obj, 1L);
			long cc = System.currentTimeMillis();
			idField.setAccessible(false);
			long dd = System.currentTimeMillis();
			
			a += bb-aa;
			b += cc-bb;
			c += dd-cc;
		}
		long end = System.currentTimeMillis();
		System.out.println("直接打开Accessible,对属性进行赋值,耗时:");
		System.out.println("idField.setAccessible(true):" + a);
		System.out.println("idField.set(obj, 1L):" + b);
		System.out.println("idField.setAccessible(false):" + c);
		System.out.println("整个耗时:" + (end - start));
	}

用set方法赋值:

/**
	 * 使用set方法赋值
	 * @throws Exception
	 */
	@Test
	public void testMethodSet() throws Exception {
		Class<User> clazz = User.class;
		Field idField = clazz.getDeclaredField("id");
		Object obj = clazz.newInstance();
		long start = System.currentTimeMillis();
		long a = 0,b = 0,c = 0;
		for(int i=0;i<10000000;i++){
			long aa = System.currentTimeMillis();
			String theSetMethodName = ClassUtils.toSetMethodName(idField.getName());
			long bb = System.currentTimeMillis();
			Method theSetMethod = clazz.getMethod(theSetMethodName, idField.getType());
			long cc = System.currentTimeMillis();
			theSetMethod.invoke(obj, 1L);
			long dd = System.currentTimeMillis();
			
			a += bb-aa;
			b += cc-bb;
			c += dd-cc;
		}
		long end = System.currentTimeMillis();
		System.out.println("使用set方法赋值,耗时:");
		System.out.println("String theSetMethodName = ClassUtils.toSetMethodName(idField.getName()):" + a);
		System.out.println("Method theSetMethod = clazz.getMethod(theSetMethodName, idField.getType()):" + b);
		System.out.println("theSetMethod.invoke(obj, 1L):" + c);
		System.out.println("整个耗时:" + (end - start));
	}

======================= 分割线============================

重复赋值10000000次的结果:



结果自己看。。就不解释了。。


再来看看对属性进行取值吧:

打开Accessible,对属性取值:

/**
	 * 直接打开Accessible,对属性进行取值
	 * @throws Exception
	 */
	@Test
	public void testFieldGet() throws Exception {
		User obj = new User();
		obj.setId(1L);
		Field idField = User.class.getDeclaredField("id");
		long start = System.currentTimeMillis();
		long a = 0,b = 0,c = 0;
		for(int i=0;i<10000000;i++){
			long aa = System.currentTimeMillis();
			idField.setAccessible(true);
			long bb = System.currentTimeMillis();
			idField.get(obj);
			long cc = System.currentTimeMillis();
			idField.setAccessible(false);
			long dd = System.currentTimeMillis();
			
			a += bb-aa;
			b += cc-bb;
			c += dd-cc;
		}
		long end = System.currentTimeMillis();
		System.out.println("直接打开Accessible,对属性进行取值,耗时:");
		System.out.println("idField.setAccessible(true):" + a);
		System.out.println("idField.set(obj, 1L):" + b);
		System.out.println("idField.setAccessible(false):" + c);
		System.out.println("整个耗时:" + (end - start));
	}


用get方法进行赋值:

/**
	 * 使用get方法取值
	 * @throws Exception
	 */
	@Test
	public void testMethodGet() throws Exception {
		User obj = new User();
		obj.setId(1L);
		Field idField = User.class.getDeclaredField("id");
		long start = System.currentTimeMillis();
		long a = 0,b = 0,c = 0;
		for(int i=0;i<10000000;i++){
			long aa = System.currentTimeMillis();
			String theGetMethodName = ClassUtils.toGetMethodName(idField.getName());
			long bb = System.currentTimeMillis();
			Method theGetMethod = User.class.getMethod(theGetMethodName);
			long cc = System.currentTimeMillis();
			theGetMethod.invoke(obj);
			long dd = System.currentTimeMillis();
			
			a += bb-aa;
			b += cc-bb;
			c += dd-cc;
		}
		long end = System.currentTimeMillis();
		System.out.println("使用get方法取值,耗时:");
		System.out.println("String theSetMethodName = ClassUtils.toSetMethodName(idField.getName()):" + a);
		System.out.println("Method theSetMethod = clazz.getMethod(theSetMethodName, idField.getType()):" + b);
		System.out.println("theSetMethod.invoke(obj, 1L):" + c);
		System.out.println("整个耗时:" + (end - start));
	}


======================= 分割线============================

重复取值10000000次的结果:



尼玛。。我又不说话了。。自己看数据


======================= 分割线============================

好了,到了提问时间:

为什么Struts2的IOC,必须我们提供set方法呢? 研究过Struts2源码的同学请回答...(我知道Spring注入是不用提供的,不是鄙视Struts2,只是求解而已。)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值