属性copy性能比较

来源:互联网 发布:成人频道直播软件 编辑:程序博客网 时间:2024/05/17 07:23
两个bean之间有共同的属性,要实现相同属性间赋值,可以使用set,get,这种方法是最高效的。如果bean含有大量的属性,并且赋值,这样最写大量的get,set,代码不利于维护,PropertyUtils,BeanUtils,PropertyUtilsBean都提供了copyProperties方法,当然也可以自己通过反射来自己实现,各性能如下:
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方法。