属性copy性能比较
来源:互联网 发布:成人频道直播软件 编辑:程序博客网 时间:2024/05/17 07:23
两个bean之间有共同的属性,要实现相同属性间赋值,可以使用set,get,这种方法是最高效的。如果bean含有大量的属性,并且赋值,这样最写大量的get,set,代码不利于维护,PropertyUtils,BeanUtils,PropertyUtilsBean都提供了copyProperties方法,当然也可以自己通过反射来自己实现,各性能如下:
log4j:WARN unable to find the env.properties file
name=zhangsan,age=26,date=Thu Nov 28 21:43:25 CST 2013
propertyCopy方法用时:238577700纳秒
name=zhangsan,age=26,date=Thu Nov 28 21:43:25 CST 2013
beanUtils方法用时:792249纳秒
name=zhangsan,age=26,date=Thu Nov 28 21:43:25 CST 2013
PropertyUtilsBean方法用时:128124纳秒
name=zhangsan,age=26,date=Thu Nov 28 21:43:25 CST 2013
Student s1 = new Student();s1.setName("zhangsan");s1.setAge(26);s1.setBirthDay(new Date());Student s2 = new Student();long start = System.nanoTime();s2.setAge(s1.getAge());s2.setName(s1.getName());long end = System.nanoTime(); System.out.println("set方法用时:" + (end - start));Student s3 = new Student();start = System.nanoTime();try{PropertyUtils.copyProperties(s3, s1);} catch (Exception e){}end = System.nanoTime();System.out.println("name=" + s3.getName() + ",age=" + s3.getAge() + ",date=" + s3.getBirthDay());System.out.println("propertyCopy方法用时:" + (end - start)); Student s4 = new Student();start = System.nanoTime();try{BeanUtils.copyProperties(s4, s1); } catch (Exception e){}end = System.nanoTime();System.out.println("name=" + s4.getName() + ",age=" + s4.getAge() + ",date=" + s4.getBirthDay()); System.out.println("beanUtils方法用时:" + (end - start)); Student s5 = new Student();start = System.nanoTime();try{PropertyUtilsBean.class.newInstance().copyProperties(s5, s1);} catch (Exception e){}end = System.nanoTime();System.out.println("name=" + s5.getName() + ",age=" + s5.getAge() + ",date=" + s5.getBirthDay()); System.out.println("PropertyUtilsBean方法用时:" + (end - start));Student s6 = new Student();start = System.nanoTime();try{copyProperties(s6, s1);} catch (Exception e){}end = System.nanoTime();System.out.println("name=" + s6.getName() + ",age=" + s6.getAge() + ",date=" + s6.getBirthDay()); System.out.println("自己实现方法用时:" + (end - start));自己实现copy代码
public static Object copyProperties(Object dbInstance, Object bean) { Class klass = bean.getClass(); Object obj = null; Method methods[] = klass.getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; String name = method.getName(); String key = ""; if (name.startsWith("get") && !name.equals("getClass")) { key = name.substring(3); } else { if (!name.startsWith("is")) { continue; } key = name.substring(2); } try { obj = method.invoke(bean, new Object[]{}); } catch (Exception e) { e.printStackTrace(); } if (obj != null && !obj.getClass().getName().equals("java.util.HashSet")) { try { for (int j = 0; j < methods.length; j++) { if (!methods[j].getName().equals( (new StringBuilder("set")).append(key) .toString())) { continue; } Object objs[] = new Object[1]; objs[0] = obj; methods[j].invoke(dbInstance, objs); break; } } catch (Exception e) { key = name.substring(3); for (int j = 0; j < methods.length; j++) { if (!methods[j].getName().equals( (new StringBuilder("set")).append(key) .toString())) { continue; } Object objs[] = new Object[1]; objs[0] = obj; try { methods[j].invoke(dbInstance, objs); } catch (Exception ex) { ex.printStackTrace(); } break; } } } } return dbInstance; }
用时输出如下:
set方法用时:3642纳秒
INFO 2013-11-28 21:43:26,015 [main][] [cn.com.carsmart.util.configmanagement.ConfigUtil] - config reader type is set to default classpath loading way.log4j:WARN unable to find the env.properties file
name=zhangsan,age=26,date=Thu Nov 28 21:43:25 CST 2013
propertyCopy方法用时:238577700纳秒
name=zhangsan,age=26,date=Thu Nov 28 21:43:25 CST 2013
beanUtils方法用时:792249纳秒
name=zhangsan,age=26,date=Thu Nov 28 21:43:25 CST 2013
PropertyUtilsBean方法用时:128124纳秒
name=zhangsan,age=26,date=Thu Nov 28 21:43:25 CST 2013
自己实现方法用时:60255纳秒
结论:对性能要求特别高的用set,get方法;只有简单属性的自己反射实现;对性能要求不太高,代码又便于维护的用PropertyUtilsBean的copyProperties方法。
- 属性copy性能比较
- BeanUtils和Cglib的Java Bean属性copy功能的性能比较
- strcpy, lstrcpy 相关API实现字符串COPY, 性能之比较
- COPY Java不同压缩算法的性能比较
- iOS开发:属性修饰符及copy与mutablecopy比较
- C#中字段与属性性能比较
- NSString的copy 属性
- property中的copy属性
- JavaBean 属性copy
- 属性修饰copy;copy、mutableCopy;NSCopying NSMutableCopying
- 性能比较
- NSString copy和retain属性
- 体会NSString的copy属性
- 初探 属性的copy、strong
- 使用COPY声明NSSTRING属性
- 使用COPY声明NSSTRING属性
- 为什么NSString属性使用copy
- copy属性的一个好处
- 新浪微博开放平台研究-实现微博自动评论(下)
- mysql导入数据库报错Error Code: 2006
- 函数传值时的内存复制
- hdu3062 Party
- windows下整合 tomcat 和 nginx
- 属性copy性能比较
- copy构造函数 和 赋值函数
- 我的青春我做主
- Evaluate Reverse Polish Notation | leetcode
- eclipse常用快捷键
- 3143 二叉树的序遍历(维基oi)
- 数据结构-串-KMP模式匹配算法
- hdu1827Summer Holiday
- Win32多线程之Interlocked Variables