最近在使用因为受不了Hibernate的各种问题,自己用jdbc在做一个简单的Orm框架 。既然涉及到Orm框架,肯定少不了使用反射对Bean进行赋值。
我最初是参考struts2的IOC,使用setXXX和getXXX进行赋值取值。但是后来发现,直接对属性赋值的性能更加好。
下面赋上我的测试代码,我对每行关键代码做了时间记录,以便清楚地看出哪些代码性能损耗高:
打开Accessible,对属性赋值:
/**
* 直接打开Accessible,对属性进行赋值
* @throws Exception
*/
@Test
public void testFieldSet() throws Exception {
Class<User> clazz = User.class;
Field idField = clazz.getDeclaredField("id");
Object obj = clazz.newInstance();
long start = System.currentTimeMillis();
long a = 0,b = 0,c = 0;
for(int i=0;i<10000000;i++){
long aa = System.currentTimeMillis();
idField.setAccessible(true);
long bb = System.currentTimeMillis();
idField.set(obj, 1L);
long cc = System.currentTimeMillis();
idField.setAccessible(false);
long dd = System.currentTimeMillis();
a += bb-aa;
b += cc-bb;
c += dd-cc;
}
long end = System.currentTimeMillis();
System.out.println("直接打开Accessible,对属性进行赋值,耗时:");
System.out.println("idField.setAccessible(true):" + a);
System.out.println("idField.set(obj, 1L):" + b);
System.out.println("idField.setAccessible(false):" + c);
System.out.println("整个耗时:" + (end - start));
}
用set方法赋值:
/**
* 使用set方法赋值
* @throws Exception
*/
@Test
public void testMethodSet() throws Exception {
Class<User> clazz = User.class;
Field idField = clazz.getDeclaredField("id");
Object obj = clazz.newInstance();
long start = System.currentTimeMillis();
long a = 0,b = 0,c = 0;
for(int i=0;i<10000000;i++){
long aa = System.currentTimeMillis();
String theSetMethodName = ClassUtils.toSetMethodName(idField.getName());
long bb = System.currentTimeMillis();
Method theSetMethod = clazz.getMethod(theSetMethodName, idField.getType());
long cc = System.currentTimeMillis();
theSetMethod.invoke(obj, 1L);
long dd = System.currentTimeMillis();
a += bb-aa;
b += cc-bb;
c += dd-cc;
}
long end = System.currentTimeMillis();
System.out.println("使用set方法赋值,耗时:");
System.out.println("String theSetMethodName = ClassUtils.toSetMethodName(idField.getName()):" + a);
System.out.println("Method theSetMethod = clazz.getMethod(theSetMethodName, idField.getType()):" + b);
System.out.println("theSetMethod.invoke(obj, 1L):" + c);
System.out.println("整个耗时:" + (end - start));
}
======================= 分割线============================
重复赋值10000000次的结果:
结果自己看。。就不解释了。。
再来看看对属性进行取值吧:
打开Accessible,对属性取值:
/**
* 直接打开Accessible,对属性进行取值
* @throws Exception
*/
@Test
public void testFieldGet() throws Exception {
User obj = new User();
obj.setId(1L);
Field idField = User.class.getDeclaredField("id");
long start = System.currentTimeMillis();
long a = 0,b = 0,c = 0;
for(int i=0;i<10000000;i++){
long aa = System.currentTimeMillis();
idField.setAccessible(true);
long bb = System.currentTimeMillis();
idField.get(obj);
long cc = System.currentTimeMillis();
idField.setAccessible(false);
long dd = System.currentTimeMillis();
a += bb-aa;
b += cc-bb;
c += dd-cc;
}
long end = System.currentTimeMillis();
System.out.println("直接打开Accessible,对属性进行取值,耗时:");
System.out.println("idField.setAccessible(true):" + a);
System.out.println("idField.set(obj, 1L):" + b);
System.out.println("idField.setAccessible(false):" + c);
System.out.println("整个耗时:" + (end - start));
}
用get方法进行赋值:
/**
* 使用get方法取值
* @throws Exception
*/
@Test
public void testMethodGet() throws Exception {
User obj = new User();
obj.setId(1L);
Field idField = User.class.getDeclaredField("id");
long start = System.currentTimeMillis();
long a = 0,b = 0,c = 0;
for(int i=0;i<10000000;i++){
long aa = System.currentTimeMillis();
String theGetMethodName = ClassUtils.toGetMethodName(idField.getName());
long bb = System.currentTimeMillis();
Method theGetMethod = User.class.getMethod(theGetMethodName);
long cc = System.currentTimeMillis();
theGetMethod.invoke(obj);
long dd = System.currentTimeMillis();
a += bb-aa;
b += cc-bb;
c += dd-cc;
}
long end = System.currentTimeMillis();
System.out.println("使用get方法取值,耗时:");
System.out.println("String theSetMethodName = ClassUtils.toSetMethodName(idField.getName()):" + a);
System.out.println("Method theSetMethod = clazz.getMethod(theSetMethodName, idField.getType()):" + b);
System.out.println("theSetMethod.invoke(obj, 1L):" + c);
System.out.println("整个耗时:" + (end - start));
}
======================= 分割线============================
重复取值10000000次的结果:
尼玛。。我又不说话了。。自己看数据
======================= 分割线============================
好了,到了提问时间:
为什么Struts2的IOC,必须我们提供set方法呢? 研究过Struts2源码的同学请回答...(我知道Spring注入是不用提供的,不是鄙视Struts2,只是求解而已。)