观前提示:
本文所使用的Eclipse版本为Photon Release (4.8.0),JDK版本为1.6.0_45。
本文仅仅为各Bean复制的性能对比,具体分析待日后补充。
1.结果分析
测试采用的是每种方法测试10次取平均值,时间单位为毫秒值。
此分析结果仅供参考,如有错误,请留言指出,谢谢。
10次 | 1000次 | 10000次 | 100000次 | |
---|---|---|---|---|
get set | 1 | 1.1 | 3.4 | 22.7 |
cglib BeanCopier | 0.1 | 0.3 | 4.4 | 9.7 |
spring BeanUtils | 154.7 | 173 | 236.5 | 281.1 |
apache PropertyUtils | 138.5 | 196.4 | 271.3 | 499.6 |
apache BeanUtils | 140.1 | 222 | 385.9 | 789.4 |
综合比较下来 get set在10000次以内效率很高, cglib BeanCopier整体效率很高,在数据量更大的时候体现的更加明显。
2.测试代码
复制的实体Person.java、PersonDto.java
package test;
public class Person {
private String id;
private String name;
private String sex;
private String address;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package test;
public class PersonDto {
private String id;
private String name;
private String sex;
private String address;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Get Set方式 GetSetTest.java
package test;
public class GetSetTest {
public static void main(String[] args) {
Person p1 = new Person();
p1.setId("0001");
p1.setName("张三");
p1.setAge(18);
p1.setSex("male");
p1.setAddress("中国");
int count = 10000;
long start = System.currentTimeMillis();
System.out.println("开始计时:" + start);
for(int i = 0; i < count; i++) {
PersonDto p2 = new PersonDto();
p2.setId(p1.getId());
p2.setName(p1.getName());
p2.setAge(p1.getAge());
p2.setSex(p1.getSex());
p2.setAddress(p1.getAddress());
}
long end = System.currentTimeMillis();
System.out.println("结束计时:" + end);
System.out.println("耗时:" + (end - start));
}
}
cglib BeanCopier 方式 CglibTest.java
package test;
import net.sf.cglib.beans.BeanCopier;
public class CglibTest {
public static void main(String[] args) {
Person p1 = new Person();
p1.setId("0001");
p1.setName("张三");
p1.setAge(18);
p1.setSex("male");
p1.setAddress("中国");
final BeanCopier copier = BeanCopier.create(Person.class, PersonDto.class, false);
int count = 10000;
long start = System.currentTimeMillis();
System.out.println("开始计时:" + start);
for(int i = 0; i < count; i++) {
PersonDto p2 = new PersonDto();
copier.copy(p1, p2, null);
}
long end = System.currentTimeMillis();
System.out.println("结束计时:" + end);
System.out.println("耗时:" + (end - start));
}
}
spring BeanUtils 方式 SpringTest.java
package test;
import org.springframework.beans.BeanUtils;
public class SpringTest {
public static void main(String[] args) {
Person p1 = new Person();
p1.setId("0001");
p1.setName("张三");
p1.setAge(18);
p1.setSex("male");
p1.setAddress("中国");
int count = 10000;
long start = System.currentTimeMillis();
System.out.println("开始计时:" + start);
for(int i = 0; i < count; i++) {
PersonDto p2 = new PersonDto();
BeanUtils.copyProperties(p1, p2);
}
long end = System.currentTimeMillis();
System.out.println("结束计时:" + end);
System.out.println("耗时:" + (end - start));
}
}
apache BeanUtils 方式 ApacheBeanUtilsTest.java
package test;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
public class ApacheBeanUtilsTest {
public static void main(String[] args) throws IllegalAccessException, InvocationTargetException {
Person p1 = new Person();
p1.setId("0001");
p1.setName("张三");
p1.setAge(18);
p1.setSex("male");
p1.setAddress("中国");
int count = 10000;
long start = System.currentTimeMillis();
System.out.println("开始计时:" + start);
for(int i = 0; i < count; i++) {
PersonDto p2 = new PersonDto();
BeanUtils.copyProperties(p1, p2);
}
long end = System.currentTimeMillis();
System.out.println("结束计时:" + end);
System.out.println("耗时:" + (end - start));
}
}
apache PropertyUtils方式 ApachePropertyUtilsTest.java
package test;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.PropertyUtils;
public class ApachePropertyUtilsTest {
public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
Person p1 = new Person();
p1.setId("0001");
p1.setName("张三");
p1.setAge(18);
p1.setSex("male");
p1.setAddress("中国");
int count = 10000;
long start = System.currentTimeMillis();
System.out.println("开始计时:" + start);
for(int i = 0; i < count; i++) {
PersonDto p2 = new PersonDto();
PropertyUtils.copyProperties(p1, p2);
}
long end = System.currentTimeMillis();
System.out.println("结束计时:" + end);
System.out.println("耗时:" + (end - start));
}
}