Test cases:
package com.cn.copy.service;import java.lang.reflect.InvocationTargetException;import java.util.Date;import org.apache.commons.beanutils.BeanUtils;import org.apache.commons.beanutils.PropertyUtils;import com.cn.copy.vo.Middle;import com.cn.copy.vo.MiddleDto;public class Test {public static void testMapObjectToObject(int count) {Middle m1 = new Middle();m1.setFlag(true);m1.setId(1000L);m1.setItem(new int[] { 10, 20, 30 });m1.setPrice(12.33);m1.setType("hahaha!");m1.setDate(new Date());long t1 = System.currentTimeMillis();for (int i = 0; i < count; i++) {MiddleDto mg = new MiddleDto();VoPoConverter.copyProperties(mg, m1);}System.out.println("DozerBeanMapper-time1:"+ (System.currentTimeMillis() - t1));}public static void testPropertyCopyObjectObject(int count) {Middle m1 = new Middle();m1.setFlag(true);m1.setId(1000L);m1.setItem(new int[] { 10, 20, 30 });m1.setPrice(12.33);m1.setType("hahaha!");m1.setDate(new Date());long t1 = System.currentTimeMillis();for (int i = 0; i < count; i++) {MiddleDto mg = new MiddleDto();try {PropertyUtils.copyProperties(mg, m1);} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println("PropertyUtils-time2:"+ (System.currentTimeMillis() - t1));}public static void testBeanCopyObjectObject(int count) {Middle m1 = new Middle();m1.setFlag(true);m1.setId(1000L);m1.setItem(new int[] { 10, 20, 30 });m1.setPrice(12.33);m1.setType("hahaha!");m1.setDate(new Date());long t1 = System.currentTimeMillis();for (int i = 0; i < count; i++) {MiddleDto mg = new MiddleDto();try {BeanUtils.copyProperties(mg, m1);} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println("BeanUtils-time3:"+ (System.currentTimeMillis() - t1));}public static void main(String[] args) {int total = 10000;Test.testMapObjectToObject(total);Test.testPropertyCopyObjectObject(total);Test.testBeanCopyObjectObject(total);}}
1000 running results:
DozerBeanMapper-time1:306PropertyUtils-time2:30BeanUtils-time3:42
100000 running results:
DozerBeanMapper-time1:2904PropertyUtils-time2:1155BeanUtils-time3:2663
1000000 running results:
DozerBeanMapper-time1:26588PropertyUtils-time2:11454BeanUtils-time3:26315
Test results:
1. beanutils is more efficient than Dozer within times
2. More than 100,000 beanutils efficiency is similar to that of Dozer.
3. The comparison is based on the basic type. If the dozer performs deep copy or associated copy, the efficiency should be slower.
4. propertyutils and beanutils functions are basically the same. The only difference is that beanutils converts the type when assigning values to beans, while propertyutils does not convert the type, if the type is different, an exception is thrown !, This explains why propertyutils is more efficient than the other two.
Suggestion: use the basic type when the source and target types are consistent:Propertyutils is more efficient. Complex types of copies can be used:Dozer.