Beanutils, propertyutils, Dozer

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.