转载自: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. NPM采用Rust以消除性能瓶颈

    Npam的指数级增长促使npm工程团队从Node.js切换到Rust,以处理那些将成为性能瓶颈的CPU绑定任务.最近的一份白皮书概述了在Rust中开发这个新服务,以及将其投入生产一年多的经验. 大部分 ...

  2. iOS Block 页面传值

    为什么80%的码农都做不了架构师?>>>   直接上代码 1.定义block @interface TopTypeCollectionView : UIView @property ...

  3. 《Android的设计与实现:卷I》——第1章 1.2.2动态视角的体系结构

    1.2.2 动态视角的体系结构静态的体系结构是从横向分层的角度诠释Android是什么.如果静态的体系结构不足以让读者理解Android的运行机制,我们可以看看Google工程师Sans Serif是 ...

  4. golang之reflect

    reflect,反射. 利用reflect,可以得到一个struct的相关信息. package main import ( "fmt" "reflect" ) ...

  5. u-boot: Not enough room for program headers, try linking with -N

    在编译u-boot的时候出现了以下错误: arm-linux-gnueabi-ld.bfd: u-boot: Not enough room for program headers, try link ...

  6. 武装你的WEBAPI-OData便捷查询

    本文属于OData系列 目录(可能会有后续修改) 武装你的WEBAPI-OData入门 武装你的WEBAPI-OData便捷查询 武装你的WEBAPI-OData分页查询 武装你的WEBAPI-ODa ...

  7. FPGA六位共阳极数码管动态显示

    `timescale 1ns/1ps module adc_dis( clk , rst_n , sm_seg , sm_bit ); input clk;//50HZ input rst_n; :] ...

  8. (1)从通信中的MCS含义开始讲起

    通信中的MCS:Modulation and Coding Scheme,意思为调制编码方案/调制编码策略,其内涵可分为两个部分:Modulation  和  Coding. 在基带的信号处理流程中, ...

  9. about VennsBlog.

    此博客主要将用于记录自己学习路上的一些点滴及心得 同时,也希望各位提出指正 相互交流,共同进步 感谢相遇

  10. JS理论-跨域解决方案

    一: 用过JS跨域 1.JSONP跨域(利用script标签不受网站同源策略影响) 2.documen.domian跨域(通过指定基础域名,达到在一个域) 二: 通过服务器跨域 1.通过代理服务器,比 ...