日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

java反射效率对比_Java反射三种方式的效率对比

發(fā)布時(shí)間:2024/1/23 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java反射效率对比_Java反射三种方式的效率对比 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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 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 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 耗時(shí)對比,在使用相同的查詢條件,相同的數(shù)據(jù)的情況下。通過實(shí)驗(yàn)得到以下兩組數(shù)據(jù)(耗時(shí)/納秒)

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

每組數(shù)據(jù)前的數(shù)字代表之前取值的方式。

由數(shù)據(jù)對比可以看出,耗時(shí)最小的,始終是方式1,并且方式1在多次調(diào)用時(shí),耗時(shí)是逐步減少的,可能是有緩存機(jī)制。

耗時(shí)第二少的是方式3,耗時(shí)最多的是方式2.

5 解析

方式1,采用了最基本的java反射方式,使用Filed,循環(huán)bean的Field,得到Object,再轉(zhuǎn)換為Integer類型。 方式2,采用了看似最簡潔的BeanUitls,根據(jù)屬性名,獲取屬性值。這樣最耗時(shí)。 方式3,采用了獲取bean的Method,然后invoke的方式來獲取值。

總結(jié)

以上是生活随笔為你收集整理的java反射效率对比_Java反射三种方式的效率对比的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。