Spring BeanUtils, cglib BeanCopier, apache BeanUtils PropertyUtils Bean replication performance comparison

Spring BeanUtils, cglib BeanCopier, apache BeanUtils PropertyUtils Bean replication performance comparison

Tips before viewing:

The Eclipse version used in this article is Photon Release (4.8.0), and the JDK version is 1.6.0_45.

This article only compares the performance of each Bean replication, the specific analysis will be added in the future.

1. Result analysis

The test uses each method to test 10 times to average, and the time unit is milliseconds.

This analysis result is for reference only. If there is any error, please leave a message to point it out, thank you.

10 times 1000 times 10000 times 100000 times
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

In a comprehensive comparison, get set is very efficient within 10,000 times. The overall efficiency of cglib BeanCopier is very high, which is more obvious when the amount of data is larger.

2. Test the code

The copied entities 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 method 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 method 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 way 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 method 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 method 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));
	}
}

Guess you like

Origin blog.csdn.net/weixin_43611145/article/details/106840000