对比BeanUtils、PropertyUtils、BeanCopier的性能消耗

主要代码

定义2个bean对象:

public class copyPropertiesData1 {
private Integer id;

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
public class copyPropertiesData2 {
private Integer id;

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}

}

  

分别使用BeanUtils、PropertyUtils、BeanCopier转化1000个对象:

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import net.sf.cglib.beans.BeanCopier;
//import org.springframework.beans.BeanUtils;

public class copyPropertiesCompare {
	final static List<copyPropertiesData1> entityList;
	static{
		entityList  = new ArrayList();
        for (int i = 0; i < 10; i++) {
        	copyPropertiesData1 entity = new copyPropertiesData1();
            entityList.add(entity);
        }
	}
	//使用BeanUtils转化1000个对象
	public void test_convert_entity_to_model_performance_use_beanutils() throws IllegalAccessException, InvocationTargetException{
        long start = System.currentTimeMillis();
        List<copyPropertiesData2> modelList = new ArrayList<>();
        for (copyPropertiesData1 src : entityList) {
        	copyPropertiesData2 dest = new copyPropertiesData2();
            BeanUtils.copyProperties(src, dest);
            modelList.add(dest);
        }
        System.out.printf("BeanUtils took time: %d(ms)%n",System.currentTimeMillis() - start);
    }

	//使用PropertyUtils转化1000个对象
		public void test_convert_entity_to_model_performance_use_PropertyUtils() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
	        long start = System.currentTimeMillis();
	        List<copyPropertiesData2> modelList = new ArrayList<>();
	        for (copyPropertiesData1 src : entityList) {
	        	copyPropertiesData2 dest = new copyPropertiesData2();
	        	PropertyUtils.copyProperties(src, dest);
	            modelList.add(dest);
	        }
	        System.out.printf("PropertyUtils took time: %d(ms)%n",System.currentTimeMillis() - start);
	    }

	//使用BeanCopier转化1000个对象
	public void test_convert_entity_to_model_performance_use_beancopier(){
        long start = System.currentTimeMillis();
        BeanCopier b = BeanCopier.create(copyPropertiesData1.class, copyPropertiesData2.class, false);
        List<copyPropertiesData2> modelList = new ArrayList<>();
        for (copyPropertiesData1 src : entityList) {
        	copyPropertiesData2 dest = new copyPropertiesData2();
            b.copy(src, dest, null);
            modelList.add(dest);
        }
        System.out.printf("BeanCopier took time: %d(ms)%n",System.currentTimeMillis() - start);
    }

	public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
		copyPropertiesCompare test = new copyPropertiesCompare();
		test.test_convert_entity_to_model_performance_use_beanutils();
//		test.test_convert_entity_to_model_performance_use_PropertyUtils();
//		test.test_convert_entity_to_model_performance_use_beancopier();

	}
}

  

多线程调用:

import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecuteTest {
	public static void main(String[] args){
		BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(10, true);
		ExecutorService executorService = new ThreadPoolExecutor(10, 10, 1, TimeUnit.MINUTES, workQueue, new ThreadPoolExecutor.CallerRunsPolicy());
		for(int i=1;i<9999999;i++){
			executorService.execute(new ExecuteTtest());
		}
		executorService.shutdown();
	}

	static class ExecuteTtest implements Runnable {

        private ExecuteTtest() {
        }

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
            System.out.println(Thread.currentThread().getName() + " thread start to work");
            copyPropertiesCompare test = new copyPropertiesCompare();
//            test.test_convert_entity_to_model_performance_use_beancopier();
//            test.test_convert_entity_to_model_performance_use_beanutils();
            test.test_convert_entity_to_model_performance_use_PropertyUtils();
        } catch (Exception e) {
            e.printStackTrace();
        }
	}

}
}

  

结果

  • 单笔调用耗时如下:

BeanUtils took time: 110(ms)
PropertyUtils took time: 5(ms)
BeanCopier took time: 75(ms)

  • 并发调用消耗cpu情况

BeanUtils.copyProperties():

PropertyUtils.copyProperties():

cglib beans.BeanCopier():

原文地址:https://www.cnblogs.com/blackjasmine/p/10264863.html

时间: 2024-07-31 12:28:53

对比BeanUtils、PropertyUtils、BeanCopier的性能消耗的相关文章

对象拷贝类PropertyUtils,BeanUtils,BeanCopier的技术沉淀

对象拷贝类PropertyUtils,BeanUtils,BeanCopier的技术沉淀 性能对比: BeanCopier > PropertyUtils > BeanUtils. 其中BeanCopier的性能高出另外两个100数量级. BeanCopier使用可参考: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp36 对象拷贝的应用现状简介: 业务系统中经常需要两个对象进行属性的拷贝,不能否认逐个的对象拷贝是最快速最

Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier)

 文章转载来自:http://www.cnblogs.com/kaka/archive/2013/03/06/2945514.html 作为一个新员工,一个首要的工作就是阅读别人的代码,阅读代码的诸多好处就不说了,我就直奔主题,通过预读代码,发现了几种实现两个不同类型的Bean之 间实现值复制的几种方式,上网查询后发现性能上会有差异,于是就萌生自己深入了解几种实现的想法.第一步就是先本着实事求是的原则去探求一下大家总结出来 的性能差异是否正确. 比较的是四种复制的方式,分别为Apache的Bea

对比归并排序和快速排序的性能

对比归并排序和快速排序的性能 实验数据: {99,95,90,88,85,83,80,75,70,65,60,55,50} 伪代码表示: 核心代码: 实现代码: #include<iostream> using namespace std; /************声明归并排序函数*****************/ void Merge(int r[],int r1[],int s,int m,int t); void MergeSort(int r[],int s,int t); /**

innerHTML对比DOM方法,哪个性能好?有什么区别?

innerHTML对比DOM方法,哪个性能好?有什么区别?是个老生常谈的问题!跟浏览器版本与发展有关! innerHTML是w3c制定的行业标准,几乎所有浏览器都做了支持: 作为原生支持,innerHTML在各主流浏览器下的执行效率是很高的: 但是他有他的缺陷,特别是在ie低版本浏览器下,不支持html5.还有一些其他的标签属性. 直接测试的话,速度明显innerHTML比DOM方法要快很多(包括撸代码的速度),但是有说在webkit内核的浏览器中DOM方法比innerHTML要快. 平时采用j

Java Collection 集合类大小调整带来的性能消耗

Java Collection类的某些具体实现由于底层数据存储基于数组,随着元素数量的增加,调整大小的代价很大.随着Collection元素增长到某个上限,调整其大小可能出现性能问题. 当Collection元素达到内部数组达到最大值后,需要创建新数组,并且将旧数组元素通过Arrays.copyOf方法拷贝到新数组,这就消耗了CPU时间片,并且还需要进行垃圾回收,特别是当Collection对象生命周期较长,已经处于老年代,需要经历一次Full GC才能释放内存.新数组的内存会在年轻代的Eden

封装 UnityEngine.Debug.Log 为Dll ,游戏发布关闭 Log 减少性能消耗

本文参考雨松Mono的文章: http://www.xuanyusong.com/archives/2782 Mono介绍的是Mac 上Console 编译DLL的方法,本文是在 Win7 系统使用MonoDeveloper 编译. 文章转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn/ 在游戏发布时,有很多东西需要进行优化 ,消耗性能的东西 能减少就减少. UnityEngine.Debug.Log 是很消耗性能的操作,即使我们

JS性能消耗在哪里?

内部原因:构造,递归,循环,拷贝,动态执行,字符串操作等 1.过度的封装(过多的创建“庞大的”对象,但是如果在允许的条件下,面向对象的封装是可以提高维护性,而且符合我们的高内聚低耦合原则): 2.少的去使用私有变量在外界访问(向外界通过暴漏接口的方式去访问私有变量很消耗性能): 3.大量构造对象开销很大: 4.动态使用脚本的增加DOM节点改变DOM的拓扑结构在“互联网”当中更是不可取的(可以通过隐藏显示方法): 5.及时释放闭包内没有释放的资源 6.能不用eval动态解析的时候尽量不用 7.简化

使用MiniProfiler跟踪MVC + EF + Bootstrap 2 权限管理系统的性能消耗

安装MiniProfiler 在MVC + EF + Bootstrap 2 权限管理系统入门级(附源码)文章中下载了它的源码,调试模式下打开一个页面都要再2.5秒以上,所以使用MiniProfiler.MiniProfiler.MVC4 .MiniProfiler.EF6组件进行了分析. 首先,依次序安装组件.然后,修改Global.aspx.cs 文件: protected void Application_Start() { AreaRegistration.RegisterAllArea

【测试】通过SYS用户,对SCOTT用户的会话进行跟踪,并分析此会话中性能消耗较高的SQL,分析并给出优化建议。

①连接到scott下,查询scott对应的sid,serial# SQL> select sid,serial#,username from v$session where username='SCOTT'; SID SERIAL# USERNAME ---------- ---------- ------------------------------ 133 15 SCOTT ②开启对scott用户的跟踪: SQL>exec dbms_system.set_sql_trace_in_ses