关于BeanUtil.copyProperties性能

BeanUtil.copyProperties 方法确实很方便,代码写出来非常优美,不会有很多的get set,但由于用到反射,可能存在潜在的性能问题。因此对spring 和 commons的BeanUtil.copyProperties进行了对照测试,代码大致如下:

@Test
public void copyPropertiesTest() throws IllegalAccessException, InvocationTargetException {

po = new PromotionPO();
Timestamp now = new Timestamp(System.currentTimeMillis());
model = RetailmodelFactory.eINSTANCE.createPromotion();
//get/set
Long startTime = System.currentTimeMillis();
for (int i = 0; i < LOOP_NUMBER; i++) {
model.setCode("code");
model.setDescription("haha");
model.setDiscount(5.4);
model.setEndTime(now);
model.setID(39578395L);
model.setPriority(1);
model.setPromotionType(3);
model.setStartTime(now);
model.setSupplierID("123245L");
}
Long endTime = System.currentTimeMillis();
traditionalCopyTIme = endTime - startTime;

//spring
startTime = System.currentTimeMillis();
for (int i = 0; i < LOOP_NUMBER; i++) {
BeanUtils.copyProperties(model, po);
}
endTime = System.currentTimeMillis();
springCopyPropertiesTime = endTime - startTime;

//commons
startTime = System.currentTimeMillis();
for (int i = 0; i < LOOP_NUMBER; i++) {
org.apache.commons.beanutils.BeanUtils.copyProperties(po,model);
}
endTime = System.currentTimeMillis();
commonsCopyPropertiesTime = endTime - startTime;

logger.info("normal copy Time: " + traditionalCopyTIme + "ms.");
logger.info("Spring BeanUtil copy Time: " + springCopyPropertiesTime + "ms.");
logger.info("Commons BeanUtil copy Time: " + commonsCopyPropertiesTime + "ms.");
}

当 LOOP_NUMBER = 50000时(即执行50000次copy),结果如下:
[table]
|编号|set/get|Spring BeanUtil|Commons BeanUtil|
|1|62ms|1703ms|63860ms|
|1|16ms|1359ms|62328ms|
|1|16ms|1359ms|63047ms|
|1|0ms|1390ms|63047ms|
|1|16ms|1375ms|62266ms|
[/table]

可看出Commons BeanUtil的copyProperties 性能最差,几乎不能忍受。spring的copyProperties有可能在接受范围内。直接set/get虽然丑陋,但是性能确实非常好,几乎可以忽略。
接下来对spring和commons两种不同实现进行分析。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值