转载自:https://blog.csdn.net/aitcax/article/details/52694423

1 使用field(效率最高)
            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 使用 org.apache.commons.beanutils.BeanUtils来获取属性
            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  使用java.lang.reflect.Method获取值
 
 
            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 耗时对比,在使用相同的查询条件,相同的数据的情况下。通过实验得到以下两组数据(耗时/纳秒)
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
每组数据前的数字代表之前取值的方式。
由数据对比可以看出,耗时最小的,始终是方式1,并且方式1在多次调用时,耗时是逐步减少的,可能是有缓存机制。
耗时第二少的是方式3,耗时最多的是方式2.
5 解析
方式1,采用了最基本的java反射方式,使用Filed,循环bean的Field,得到Object,再转换为Integer类型。
方式2,采用了看似最简洁的BeanUitls,根据属性名,获取属性值。这样最耗时。
方式3,采用了获取bean的Method,然后invoke的方式来获取值。
---------------------
作者:aitcax
来源:CSDN
原文:https://blog.csdn.net/aitcax/article/details/52694423

Java——反射三种方式的效率对比的更多相关文章

  1. C#实例化对象的三种方式及性能对比

    前言 做项目过程中有个需求要实例化两万个对象并添加到List中,这个过程大概需要1min才能加载完(传参较多),于是开启了代码优化之旅,再此记录. 首先想到的是可能实例化比较耗时,于是开始对每种实例化 ...

  2. Dynamics CRM2016 查询数据的三种方式的性能对比

    之前写过一个博客,对非声明验证方式下连接组织服务的两种方式的性能进行了对比,但当时只是对比了实例化组织服务的时间,并没有对查询数据的时间进行对比,那有朋友也在我的博客中留言了反映了查询的时间问题,一直 ...

  3. java中三种方式获得类的字节码文件对象

    package get_class_method; public class ReflectDemo { /** * @param args */ public static void main(St ...

  4. java多线程三种方式

    java多线程都有几种方式 有三种: (1)继承Thread类,重写run函数 创建: class xx extends Thread{ public void run(){ Thread.sleep ...

  5. java 读取文件内容 三种形式及效率对比

    IOUtils.getStringFromReader() 读取方式为最快的 InputStream in = null; String line = ""; long start ...

  6. Java遍历List5种方法的效率对比

    package com.test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** ...

  7. java反射三种获得类类型的方法

    public class Test { public static void main(String[] args) { Test t=new Test();//所有的类都是Class类的实例(类类型 ...

  8. Java反射机制(创建Class对象的三种方式)

    1:SUN提供的反射机制的类: java.lang.Class<T> java.lang.reflect.Constructor<T> java.lang.reflect.Fi ...

  9. 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比

    [原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...

随机推荐

  1. document.documentElement.scrollTop指定位置失效解决办法

    近期在vue的H5项目中,做指定位置定位的时候发现使用document.documentElement.scrollTop一直不生效. 解决办法是document.documentElement.sc ...

  2. CF1288C-Two Arrays (DP)

    You are given two integers n and m. Calculate the number of pairs of arrays (a,b) such that: the len ...

  3. ACM学习心得

    今天打比赛,调整好了心态,不管rank榜,所以做的比上次好,今天A了四个题,都很水,memset的清零时间,需要好长,因为memset 跟cin超时了,它的数据量1e6,所以超时了还是多用scanf, ...

  4. 数学--数论-- HDU 2601 An easy problem(约束和)

    Problem Description When Teddy was a child , he was always thinking about some simple math problems ...

  5. checked 完整版全选,单选,反选

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel= ...

  6. weak_ptr

    #include <iostream> #include <memory> using namespace std; int main(int argc, char **arg ...

  7. CTF-Reverse-[GXYCTF2019]luck_guy

    CTF-Reverse-[GXYCTF2019]luck_guy 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢!本文仅用于学习与 ...

  8. Oracle触发器之系统触发器

    系统触发器 可以用系统触发器记录一些ddl的数据操作或者是数据库的登录 或者登出操作. 语法: create or replace trigger 触发器名称 before/after 触发器时机 事 ...

  9. Mybatis-入门演示

    MyBatis:持久层框架 前言 之前有看过和学习一些mybatis的文章和内容,但是没有去写过文章记录下,现在借鉴b站的狂神视频和官方文档看来重新撸一遍入门.有错误请多指教. 内容 数据访问层-相当 ...

  10. [hdu5402 Travelling Salesman Problem]YY

    题意:给一个n*m的矩形,每个格子有一个非负数,求一条从(1,1)到(n,m)的路径(不能经过重复的格子),使得经过的数的和最大,输出具体的方案 思路:对于row为奇数的情况,一行行扫下来即可全部走完 ...