Java——反射三种方式的效率对比
转载自:https://blog.csdn.net/aitcax/article/details/52694423
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));
for (String str : dateList) {
Integer value = getMinuteAccessCount(str, callCountList);
resultList.add(value);
}
long end = System.nanoTime();
log.info("new call cost :" + (end-start));
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;
}
for (String str : dateList) {
Integer value = getMinuteAccessCount(str, callCountList);
resultList.add(value);
}
long end = System.nanoTime();
log.info("new call cost :" + (end-start));
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;
}
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 :794016
3 new call cost :912382
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 :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.
方式2,采用了看似最简洁的BeanUitls,根据属性名,获取属性值。这样最耗时。
方式3,采用了获取bean的Method,然后invoke的方式来获取值。
---------------------
作者:aitcax
来源:CSDN
原文:https://blog.csdn.net/aitcax/article/details/52694423
Java——反射三种方式的效率对比的更多相关文章
- C#实例化对象的三种方式及性能对比
前言 做项目过程中有个需求要实例化两万个对象并添加到List中,这个过程大概需要1min才能加载完(传参较多),于是开启了代码优化之旅,再此记录. 首先想到的是可能实例化比较耗时,于是开始对每种实例化 ...
- Dynamics CRM2016 查询数据的三种方式的性能对比
之前写过一个博客,对非声明验证方式下连接组织服务的两种方式的性能进行了对比,但当时只是对比了实例化组织服务的时间,并没有对查询数据的时间进行对比,那有朋友也在我的博客中留言了反映了查询的时间问题,一直 ...
- java中三种方式获得类的字节码文件对象
package get_class_method; public class ReflectDemo { /** * @param args */ public static void main(St ...
- java多线程三种方式
java多线程都有几种方式 有三种: (1)继承Thread类,重写run函数 创建: class xx extends Thread{ public void run(){ Thread.sleep ...
- java 读取文件内容 三种形式及效率对比
IOUtils.getStringFromReader() 读取方式为最快的 InputStream in = null; String line = ""; long start ...
- Java遍历List5种方法的效率对比
package com.test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** ...
- java反射三种获得类类型的方法
public class Test { public static void main(String[] args) { Test t=new Test();//所有的类都是Class类的实例(类类型 ...
- Java反射机制(创建Class对象的三种方式)
1:SUN提供的反射机制的类: java.lang.Class<T> java.lang.reflect.Constructor<T> java.lang.reflect.Fi ...
- 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比
[原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...
随机推荐
- document.documentElement.scrollTop指定位置失效解决办法
近期在vue的H5项目中,做指定位置定位的时候发现使用document.documentElement.scrollTop一直不生效. 解决办法是document.documentElement.sc ...
- 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 ...
- ACM学习心得
今天打比赛,调整好了心态,不管rank榜,所以做的比上次好,今天A了四个题,都很水,memset的清零时间,需要好长,因为memset 跟cin超时了,它的数据量1e6,所以超时了还是多用scanf, ...
- 数学--数论-- HDU 2601 An easy problem(约束和)
Problem Description When Teddy was a child , he was always thinking about some simple math problems ...
- checked 完整版全选,单选,反选
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel= ...
- weak_ptr
#include <iostream> #include <memory> using namespace std; int main(int argc, char **arg ...
- CTF-Reverse-[GXYCTF2019]luck_guy
CTF-Reverse-[GXYCTF2019]luck_guy 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢!本文仅用于学习与 ...
- Oracle触发器之系统触发器
系统触发器 可以用系统触发器记录一些ddl的数据操作或者是数据库的登录 或者登出操作. 语法: create or replace trigger 触发器名称 before/after 触发器时机 事 ...
- Mybatis-入门演示
MyBatis:持久层框架 前言 之前有看过和学习一些mybatis的文章和内容,但是没有去写过文章记录下,现在借鉴b站的狂神视频和官方文档看来重新撸一遍入门.有错误请多指教. 内容 数据访问层-相当 ...
- [hdu5402 Travelling Salesman Problem]YY
题意:给一个n*m的矩形,每个格子有一个非负数,求一条从(1,1)到(n,m)的路径(不能经过重复的格子),使得经过的数的和最大,输出具体的方案 思路:对于row为奇数的情况,一行行扫下来即可全部走完 ...