转自:http://www.cnblogs.com/kaka/archive/2013/03/06/2945514.html
比较的是四种复制的方式,分别为Apache的BeanUtils和PropertyUtils,Spring的BeanUtils,Cglib的BeanCopier。做法是在Eclipse新建了一个Project,专门用于专门测试几种代码的性能。具体的代码如下:
       一个FromBean和一个ToBean,两个的代码基本上一样,除了类名称不一样,所以只是贴出来了一份。

public class FromBean {
    private String name;
    private int age;
    private String address;
    private String idno;
    private double money;
 
    public double getMoney() {
        return money;
    }
 
    public void setMoney(double money) {
        this.money = money;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public String getAddress() {
        return address;
    }
 
    public void setAddress(String address) {
        this.address = address;
    }
 
    public String getIdno() {
        return idno;
    }
 
    public void setIdno(String idno) {
        this.idno = idno;
    }
 
}

  一个用于测试的BenchmarkTest类,为了减少重复代码,写了一个策略模式

public class BenchmarkTest {
private int count; public BenchmarkTest(int count) {
this.count = count;
System.out.println("性能测试" + this.count + "==================");
} public void benchmark(IMethodCallBack m, FromBean frombean) {
try {
long begin = new java.util.Date().getTime();
ToBean tobean = null;
System.out.println(m.getMethodName() + "开始进行测试");
for (int i = 0; i < count; i++) { tobean = m.callMethod(frombean); }
long end = new java.util.Date().getTime();
System.out.println(m.getMethodName() + "耗时" + (end - begin));
System.out.println(tobean.getAddress());
System.out.println(tobean.getAge());
System.out.println(tobean.getIdno());
System.out.println(tobean.getMoney());
System.out.println(tobean.getName());
System.out.println(" ");
} catch (Exception e) {
e.printStackTrace();
}
}
}

策略中使用的接口声明

public interface IMethodCallBack {

    String getMethodName();

    ToBean callMethod(FromBean frombean)  throws Exception;

}
   

使用的测试类

public class TestMain {

    /**
* @param args
*/
public static void main(String[] args) {
FromBean fb = new FromBean();
fb.setAddress("北京市朝阳区大屯路");
fb.setAge(20);
fb.setMoney(30000.111);
fb.setIdno("110330219879208733");
fb.setName("测试"); IMethodCallBack beanutilCB = new IMethodCallBack() { @Override
public String getMethodName() {
return "BeanUtil.copyProperties";
} @Override
public ToBean callMethod(FromBean frombean) throws Exception { ToBean toBean = new ToBean();
BeanUtils.copyProperties(toBean, frombean);
return toBean;
}
}; IMethodCallBack propertyCB = new IMethodCallBack() { @Override
public String getMethodName() {
return "PropertyUtils.copyProperties";
} @Override
public ToBean callMethod(FromBean frombean) throws Exception {
ToBean toBean = new ToBean();
PropertyUtils.copyProperties(toBean, frombean);
return toBean;
}
}; IMethodCallBack springCB = new IMethodCallBack() { @Override
public String getMethodName() {
return "org.springframework.beans.BeanUtils.copyProperties";
} @Override
public ToBean callMethod(FromBean frombean) throws Exception {
ToBean toBean = new ToBean();
org.springframework.beans.BeanUtils.copyProperties(frombean,
toBean);
return toBean;
}
}; IMethodCallBack cglibCB = new IMethodCallBack() {
BeanCopier bc = BeanCopier.create(FromBean.class, ToBean.class,
false); @Override
public String getMethodName() {
return "BeanCopier.create";
} @Override
public ToBean callMethod(FromBean frombean) throws Exception {
ToBean toBean = new ToBean();
bc.copy(frombean, toBean, null);
return toBean;
}
};

// 数量较少的时候,测试性能
BenchmarkTest bt = new BenchmarkTest(10);
bt.benchmark(beanutilCB, fb);
bt.benchmark(propertyCB, fb);
bt.benchmark(springCB, fb);
bt.benchmark(cglibCB, fb);

// 测试一万次性能测试
BenchmarkTest bt10000 = new BenchmarkTest(10000);
bt10000.benchmark(beanutilCB, fb);
bt10000.benchmark(propertyCB, fb);
bt10000.benchmark(springCB, fb);
bt10000.benchmark(cglibCB, fb);

// 担心因为顺序问题影响测试结果
BenchmarkTest bt1000R = new BenchmarkTest(10000);
bt1000R.benchmark(cglibCB, fb);
bt1000R.benchmark(springCB, fb);
bt1000R.benchmark(propertyCB, fb);
bt1000R.benchmark(beanutilCB, fb); } }

进行了三次测试,最后的结果如下:

10次测验 第一次 第二次 第三次 平均值 每次平均值
BeanUtil.copyProperties 54 57 50 53.66667 5.366666667
PropertyUtils.copyProperties 4 4 4 4 0.4
org.springframework.beans.BeanUtils.copyProperties 12 10 11 11 1.1
BeanCopier.create 0 0 0 0 0
10000次测验 第一次 第二次 第三次 平均值 每次平均值
BeanUtil.copyProperties 241 222 226 229.6667 0.022966667
PropertyUtils.copyProperties 92 90 92 91.33333 0.009133333
org.springframework.beans.BeanUtils.copyProperties 29 30 32 30.33333 0.003033333
BeanCopier.create 1 1 1 1 0.1
10000次反转测验 第一次 第二次 第三次 平均值 每次平均值
BeanUtil.copyProperties 178 174 178 176.6667 0.017666667
PropertyUtils.copyProperties 91 87 89 89 0.0089
org.springframework.beans.BeanUtils.copyProperties 21 21 21 21 0.0021
BeanCopier.create 0 1 1 0.666667 6.66667E-05

不过需要注意的是,Cglib在测试的时候,先进行了实例的缓存,这个也是他性能较好的原因之一。如果把缓存去掉的话,性能就会出现了一些的差异,但是整体的性能还是很好,不过奇怪的是10000次反而比10次少,而且后面的反转1万次反而耗时最少,进行多次测试效果也是如此。    从整体的表现来看,Cglib的BeanCopier的性能是最好的无论是数量较大的1万次的测试,还是数量较少10次,几乎都是趋近与零损耗,Spring是在次数增多的情况下,性能较好,在数据较少的时候,性能比PropertyUtils的性能差一些。PropertyUtils的性能相对稳定,表现是呈现线性增长的趋势。而Apache的BeanUtil的性能最差,无论是单次Copy还是大数量的多次Copy性能都不是很好。

  10次 10000次 10000次反转
BeanCopier.create 41 28 10

Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier)的更多相关文章

  1. Bean映射工具之Apache BeanUtils VS Spring BeanUtils

    背景 在我们实际项目开发过程中,我们经常需要将不同的两个对象实例进行属性复制,从而基于源对象的属性信息进行后续操作,而不改变源对象的属性信息,比如DTO数据传输对象和数据对象DO,我们需要将DO对象进 ...

  2. Apache BeanUtils与Spring BeanUtils性能比较

    在我们实际项目开发过程中,我们经常需要将不同的两个对象实例进行属性复制,从而基于源对象的属性信息进行后续操作,而不改变源对象的属性信息,比如DTO数据传输对象和数据对象DO,我们需要将DO对象进行属性 ...

  3. 几种流行Webservice框架性能对照

     转自[http://blog.csdn.net/thunder4393/article/details/5787121],写的非常好,以收藏. 1      摘要 开发webservice应用程序中 ...

  4. 几种流行Webservice框架性能对比

    1      摘要 开发webservice应用程序中离不开框架的支持,当open-open网站列举的就有30多种,这对于开发者如何选择带来一定的疑惑.性能Webservice的关键要素,不同的框架性 ...

  5. 几种流行Webservice框架性能对比(转载)

    1摘要 开发webservice应用程序中离不开框架的支持,当open-open网站列举的就有很多种,这对于开发者如何选择带来一定的疑惑.性能Webservice的关键要素,不同的框架性能上存在较大差 ...

  6. 背景建模技术(二):BgsLibrary的框架、背景建模的37种算法性能分析、背景建模技术的挑战

    背景建模技术(二):BgsLibrary的框架.背景建模的37种算法性能分析.背景建模技术的挑战 1.基于MFC的BgsLibrary软件下载 下载地址:http://download.csdn.ne ...

  7. EntityFramework、Dapper vs 草根框架性能大比拼,数据库访问哪家强?

    扯淡 当前市面上 ORM 很多,有跑车型的,如 Dapper,有中规中矩型的,如 Mybatis.Net,也有重量型的,如 EntityFramework 和 NHibernate,还有一些出自草根之 ...

  8. 使用四种框架分别实现百万websocket常连接的服务器

    著名的 C10K 问题提出的时候, 正是 2001 年.这篇文章可以说是高性能服务器开发的一个标志性文档,它讨论的就是单机为1万个连接提供服务这个问题,当时因为硬件和软件的**,单机1万还是一个非常值 ...

  9. EntityFramework、Dapper vs 草根框架性能

    EntityFramework.Dapper vs 草根框架性能 扯淡 当前市面上 ORM 很多,有跑车型的,如 Dapper,有中规中矩型的,如 Mybatis.Net,也有重量型的,如 Entit ...

随机推荐

  1. Android编译环境配置

    Android编译环境配置 网上关于Android编译环境配置的整理资料有不少,经整理亲测后,希望能给需要的亲们提供帮助. 主要分为四步: 1.安装JDK(Java Standard Edition ...

  2. MethodFilterInterceptor(方法拦截器)配置excludeMethors

    由于该类有setExcludeMethods方法,因此在xml中可以配置一个excludeMethods参数 刚开始老是拦截不成功,tomcat显示这个参数没找到,后来终于找到错误:不应该在拦截器栈中 ...

  3. Android异步载入全解析之开篇瞎扯淡

    Android异步载入 概述 Android异步载入在Android中使用的很广泛,除了是由于避免在主线程中做网络操作.更是为了避免在显示时由于时间太长而造成ANR,添加显示的流畅性,特别是像List ...

  4. ios学习:swift中实现分享到微博、facebook,twitter等

    在swift中打开分享功能原来是如此的简单. 1.首先须要 import Social 2.在分享button事件以下 var controller:SLComposeViewController = ...

  5. hdu4691 Front compression(后缀数组)

    Front compression Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) ...

  6. iOS使用Instrument的Leaks查找代码内存泄露

    Here are some tips for finding leaks in our project: 1. 打开Instruments调试工具控制栏, Xcode -> Open Dev T ...

  7. BZOJ3376: [Usaco2004 Open]Cube Stacking 方块游戏

    [传送门:BZOJ3376] 简要题意: 约翰和贝茜在玩一个方块游戏.编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方柱. 游戏开始后,约翰会给贝茜发出P(1≤P≤100000 ...

  8. POJ - 3847 Moving to Nuremberg 动归

    POJ - 3847 Moving to Nuremberg 题意:一张无向有权图,包括边权和点权,求一点,使得到其他点的点权*边权之和最小 思路: #pragma comment(linker, & ...

  9. update-alternatives 命令

    update-alternatives 命令 1.功能作用 update-alternatives是dpkg的实用工具,用来维护系统命令的符号链接,以决定系统默认使用什么命令. 在Debian系统中, ...

  10. Sub Thread to update main Thread (UI)

    Sub Thread to update main Thread (UI) main Thread :   A  has Hander.HandleMessage() to process the & ...