转载自: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. Linux / mac OS Shell常用命令

    一.文件.目录操作命令 1.ls命令 功能:显示文件和目录的信息 ls 以默认方式显示当前目录文件列表 ls -a 显示所有文件包括隐藏文件 ls -l 显示文件属性,包括大小,日期,符号连接,是否可 ...

  2. 基于TCP的客户端和服务器端的代码设计

    实验平台 linux 实验内容 编写TCP服务器和客户端程序,程序运行时服务器等待客户端连接.一旦连接成功,服务器显示客户端的IP地址和端口号,并向客户端发送字符串 实验原理 TCP是面向连接的通信, ...

  3. 集训模拟赛-1-T2

    好了不要在铺垫了直接整吧就 题目拿来!!!!!!! 倒水 (water) (256MB,1s) [问题描述] 你有一个水桶(记为 0),两个杯子(记为 1,2).水桶中的水量无限,容量也无限.1 号杯 ...

  4. MySQL 子查询——查询最大值

    子查询指将一个查询语句嵌套在另一个查询语句中.子查询可以在 SELECT.UPDATE 和 DELETE 语句中使用,而且可以进行多层嵌套.在实际开发时,子查询经常出现在 WHERE 子句中.子查询在 ...

  5. 配置中心Apollo搭建全过程

    总体架构 用户在Portal操作配置发布 Portal调用Admin Service的接口操作发布 Admin Service发布配置后,发送ReleaseMessage给各个Config Servi ...

  6. SpringBoot系列(十三)统一日志处理,logback+slf4j AOP+自定义注解,走起!

    往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件详解 SpringBoot系列(四)we ...

  7. PHP循环引用会遇到的坑

    今天遇到这样一个问题: 如果foreach循环一个数组,引用去对它的元素做一些操作,会有什么问题吗? 比如 [1, 2, 3],foreach循环的时候,引用给每个元素 * 2,再去foreach输出 ...

  8. Spring Cloud学习 之 Spring Cloud Ribbon 重试机制及超时设置不生效

    今天测了一下Ribbon的重试跟超时机制,发现进行的全局超时配置一直不生效,配置如下: ribbon: #单位ms,请求连接的超时时间,默认1000 ConnectTimeout: 500 #单位ms ...

  9. NEON中的L可以避免溢出

    在做加法时,比如两个255x255的数值相加,那么正确结果将是130050,对一个最大值为65565的unsigned short是会溢出的,但是如果使用L命令时,则不会产生溢出.这说明L命令,不是先 ...

  10. js数据类型很简单,却也不简单

    最近脑子里有冒出"多看点书"的想法,但我个人不是很喜欢翻阅纸质书籍,另一方面也是因为我能抽出来看书的时间比较琐碎,所以就干脆用app看电子书了(如果有比较完整的阅读时间,还是建议看 ...