BeanUtils、PropertyUtils、Dozer

测试用例:

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 block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.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 block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.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次运行结果:

DozerBeanMapper-time1:306
PropertyUtils-time2:30
BeanUtils-time3:42

100000次运行结果:

DozerBeanMapper-time1:2904
PropertyUtils-time2:1155
BeanUtils-time3:2663

1000000次运行结果:

DozerBeanMapper-time1:26588
PropertyUtils-time2:11454
BeanUtils-time3:26315

 

测试结果:

1、万次内的效率beanutils比dozer要快

2、十万次以上的效率beanutils的效率和dozer差不多。

3、以上是在只考虑基本类型的情况下比较的,dozer如果深拷贝或者关联拷贝时,效率应该会更慢。

4、PropertyUtils和BeanUtils的功能基本一致,唯一的区别是:BeanUtils在对Bean赋值时会进行类型转化,而PropertyUtils不会对类型进行转化,如果类型不同则会抛出异常!,这可以解释PropertyUtils效率比其他两种要高的原因。

建议:基本类型在源目标类型一致的情况下使用: PropertyUtils效率会更高。    复杂类型的拷贝可以使用: Dozer。

 

你可能感兴趣的:(bean,Date,String,测试,Class)