Spring BeanUtils、cglib BeanCopier、apache BeanUtils PropertyUtilsBeanレプリケーションパフォーマンスの比較

Spring BeanUtils、cglib BeanCopier、apache BeanUtils PropertyUtilsBeanレプリケーションパフォーマンスの比較

表示する前のヒント:

この記事で使用されているEclipseのバージョンはPhotonRelease(4.8.0)であり、JDKのバージョンは1.6.0_45です。

この記事では、各Beanレプリケーションのパフォーマンスのみを比較します。特定の分析は、将来追加される予定です。

1.結果分析

テストでは、各メソッドを使用して平均化するために10回テストし、時間単位はミリ秒です。

この分析結果は参考用です。エラーが発生した場合は、メッセージを残してください。ありがとうございます。

10回 1000回 10000回 100000回
設定する 1 1.1 3.43.4 22.7
cglib BeanCopier 0.1 0.3 4.4 9.7
春の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は10,000回以内で非常に効率的です。cglibBeanCopierの全体的な効率は非常に高く、データ量が多いほど顕著になります。

2.コードをテストします

コピーされたエンティティPerson.javaPersonDto.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;
	}
	
}

GetSetメソッド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));
	}
}

cglibBeanCopierメソッド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));
	}
}

春の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));
	}
}

apacheBeanUtilsメソッド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));
	}
}

apachePropertyUtilsメソッド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));
	}
}

おすすめ

転載: blog.csdn.net/weixin_43611145/article/details/106840000