Java-- reflection efficiency comparison of three ways

Reprinted from: https: //blog.csdn.net/aitcax/article/details/52694423

1 Use field (maximum efficiency)
            long start = System.nanoTime();
            Field[] fields = CallCount.class.getDeclaredFields();
            for (String str : dateList) {
                boolean exist = false;
                for (Field field : fields){
                    field.setAccessible(true);
                    if (field.getName().endsWith(str)) {
                        int value = 0;
                        for (CallCount callCount : callCountList) {
                            value += (Integer)field.get(callCount);
                        }
                        resultList.add(value);
                        exist = true;
                        break;
                    }
                }
                if (!exist) {
                    resultList.add(0);
                }
            }
            long end = System.nanoTime();
            log.info("old call cost :" + (end-start));

2 Use org.apache.commons.beanutils.BeanUtils to acquire property
            long start = System.nanoTime();
            for (String str : dateList) {
                Integer value = getMinuteAccessCount(str, callCountList);
                resultList.add(value);
            }
            long end = System.nanoTime();
            log.info("new call cost :" + (end-start));

        private Integer getMinuteAccessCount(String minute, List<CallCount> callCountList) {
        Integer result = 0;
        String propertyName = "logMinute" + minute;
        for (CallCount callCount : callCountList) {
            int value = 0;
            try {
                value = Integer.valueOf(BeanUtils.getProperty(callCount, propertyName));
            } catch (NumberFormatException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                e.printStackTrace();
            }
            result += value;
        }
        return result;
    }

3 Use java.lang.reflect.Method get value
 
 
            long start = System.nanoTime();
            for (String str : dateList) {
                Integer value = getMinuteAccessCount(str, callCountList);
                resultList.add(value);
            }
            long end = System.nanoTime();
            log.info("new call cost :" + (end-start));

        private Integer getMinuteAccessCount(String minute, List<CallCount> callCountList) {
        Integer result = 0;
        for (CallCount callCount : callCountList) {
            int value = 0;
            try {
                Method method = callCount.getClass().getDeclaredMethod("getLogMinute"+minute);
                Object obj = method.invoke(callCount);
                value = (Integer)obj;
            } catch (NumberFormatException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                e.printStackTrace();
            }
            result += value;
        }
        return result;
    }

4 Processed contrast, in the case of using the same query, the same data. To give the following two sets of data (Processed / ns) by experiment

1 old call cost :517599
1 old call cost :347916
1 old call cost :337312
1 old call cost :177893
1 old call cost :131709
1 old call cost :82789
1 old call cost :63973
3 new call cost :925383
3 new call cost :794016
3 new call cost :912382
 
1 old call cost :755016
1 old call cost :365364
1 old call cost :231944
1 old call cost :123498
1 old call cost :103315
1 old call cost :92025
1 old call cost :81762
2 new call cost :139741338
2 new call cost :3387140
2 new call cost :2230497
2 new call cost :9215854
2 new call cost :2313970
2 new call cost :1549374
2 new call cost :1884291
2 new call cost :1100880
2 new call cost :1488138
Value before each set of data before the number represents the way.
As can be seen from the comparison of the data, the minimum time-consuming, is always 1, and Mode 1 when multiple calls, time-consuming is gradually reduced, there may be a caching mechanism.
The second is less time-consuming Embodiment 3, Embodiment 2 is the most time consuming.

5 parsed

1, the most basic uses java reflection mode, using Filed, the bean cycle Field, to give Object, then converted to type Integer.
2 way, using the most seemingly simple BeanUitls, according to the attribute name, get property values. So most time-consuming.
3 embodiment, the bean acquired using Method, and then invoke a way to get the value.
---------------------
Author: aitcax
Source: CSDN
Original: https: //blog.csdn.net/aitcax/article/details/52694423

Guess you like

Origin www.cnblogs.com/it-mh/p/11268317.html
Recommended