Skip to content

Instantly share code, notes, and snippets.

@yongboy
Created January 9, 2012 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yongboy/1581399 to your computer and use it in GitHub Desktop.
Save yongboy/1581399 to your computer and use it in GitHub Desktop.
BeanCopyTest.java
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import net.sf.cglib.beans.BeanCopier;
import org.apache.commons.beanutils.BeanUtils;
/**
* 测试属性拷贝
* @author nieyong
* @time 2012-1-9
* @version 1.0
*/
public class BeanCopyTest {
private static final long NUM = 100000;
public static void main(String[] args) throws IllegalAccessException,
InvocationTargetException {
Pson p1 = new Pson();
p1.setId("1000");
p1.setName("haha!");
p1.setDate(new Date());
BeanCopier beanCopier = BeanCopier
.create(Pson.class, Pson.class, false);
long t2 = System.currentTimeMillis();
for (int i = 0; i < NUM; i++) {
Pson p2 = new Pson();
beanCopier.copy(p1, p2, null);
}
System.gc();
System.out.println("use time : " + (System.currentTimeMillis() - t2));
long t = System.currentTimeMillis();
for (int i = 0; i < NUM; i++) {
Pson p2 = new Pson();
BeanUtils.copyProperties(p2, p1);
}
System.out.println("use time : " + (System.currentTimeMillis() - t));
}
}
class Pson {
private String id;
private String name;
private Date date;
private String now;
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id
* the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the date
*/
public Date getDate() {
return date;
}
/**
* @param date
* the date to set
*/
public void setDate(Date date) {
this.date = date;
}
/**
* @return the now
*/
protected String getNow() {
return now;
}
/**
* @param now
* the now to set
*/
protected void setNow(String now) {
this.now = now;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment