Spring BeanUtils, cglib BeanCopier, apache BeanUtils PropertyUtils Comparación de rendimiento de replicación de Bean
Consejos antes de ver:
La versión de Eclipse utilizada en este artículo es Photon Release (4.8.0) y la versión de JDK es 1.6.0_45.
Este artículo solo compara el rendimiento de cada réplica de Bean, el análisis específico se agregará en el futuro.
1. Análisis de resultados
La prueba usa cada método para probar 10 veces para promediar y la unidad de tiempo es milisegundos.
El resultado de este análisis es solo de referencia. Si hay algún error, deje un mensaje para señalarlo, gracias.
10 veces | 1000 veces | 10000 veces | 100000 veces | |
---|---|---|---|---|
prepárate | 1 | 1.1 | 3.4 | 22,7 |
cglib BeanCopier | 0,1 | 0,3 | 4.4 | 9,7 |
frijoles de primavera | 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 |
En una comparación completa, get set es muy eficiente en 10,000 veces La eficiencia general de cglib BeanCopier es muy alta, lo cual es más obvio cuando la cantidad de datos es mayor.
2. Prueba el código
Las entidades copiadas 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;
}
}
Obtener el método 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));
}
}
Método 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 forma 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 método 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 método 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));
}
}