关于BeanUtil.copyProperties性能
关键字: java performance
BeanUtil.copyProperties 方法确实很方便,代码写出来非常优美,不会有很多的get set,但由于用到反射,可能存在潜在的性能问题。因此对spring 和 commons的BeanUtil.copyProperties进行了对照测试,代码大致如下:
当 LOOP_NUMBER = 50000时(即执行50000次copy),结果如下:
可看出Commons BeanUtil的copyProperties 性能最差,几乎不能忍受。spring的copyProperties有可能在接受范围内。直接set/get虽然丑陋,但是性能确实非常好,几乎可以忽略。
接下来对spring和commons两种不同实现进行分析。
- @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." );
- }
@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),结果如下:
编号 | 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 |
可看出Commons BeanUtil的copyProperties 性能最差,几乎不能忍受。spring的copyProperties有可能在接受范围内。直接set/get虽然丑陋,但是性能确实非常好,几乎可以忽略。
接下来对spring和commons两种不同实现进行分析。