java 反射 性能_Java各种反射性能对比

对各种方法实现get方法的性能进行了一个测试。

总共有5个测试,,每个测试都是执行1亿次

1. 直接通过Java的get方法

2.通过高性能的ReflectAsm库进行测试

3.通过Java Class类自带的反射获得Method测试

4.使用Java自带的Property类获取Method测试

5.BeanUtils的getProperty测试

1 测试用Bean类

测试定义了如下一个bean类。

48304ba5e6f9fe08f3fa1abda7d326ab.png

public class SimpleBean {

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

注意定义要严格遵守JavaBean规范,否则在使用和反射相关工具时会出现NoSuchMethodException异常,或者导致性能非常差,JavaBean规范中最重要的几点如下:

1.类必须是public, 拥有public无参构造器,这样能够通过反射newInstance()动态构建对象.

String className = ...;

Class beanClass = Class.forName(className);

Object beanInstance = beanClass.newInstance();

2.因为反射newInstance使用的是无参构造器, 所以对象实例化和配置是分开的

3.每一个property都有一个public的getter和setter方法, 命名方式是get/set+首字母大写的property名

经测试在SimpleBean为public时,1亿次调用method.invoke方法:

javaReflectGet 100000000 times using 218 ms

而SimpleBean为默认包可见时,1一亿次调用method.invoke方法:

javaReflectGet 100000000 times using 12955 ms

2.测试代码

48304ba5e6f9fe08f3fa1abda7d326ab.png

public class TestIterator {

private long times = 100_000_000L;

private SimpleBean bean;

private String formatter = "%s %d times using %d ms";

@Before

public void setUp() throws Exception {

bean = new SimpleBean();

bean.setName("haoyifen");

}

//直接通过Java的get方法

@Test

public void directGet() {

Stopwatch watch = Stopwatch.createStarted();

for (long i = 0; i < times; i++) {

bean.getName();

}

watch.stop();

String result = String.format(formatter, "directGet", times, watch.elapsed(TimeUnit.MILLISECONDS));

System.out.println(result);

}

//通过高性能的ReflectAsm库进行测试,仅进行一次methodAccess获取

@Test

public void reflectAsmGet() {

MethodAccess methodAccess = MethodAccess.get(SimpleBean.class);

Stopwatch watch = Stopwatch.createStarted();

for (long i = 0; i < times; i++) {

methodAccess.invoke(bean, "getName");

}

watch.stop();

String result = String.format(formatter, "reflectAsmGet", times, watch.elapsed(TimeUnit.MILLISECONDS));

System.out.println(result);

}

//通过Java Class类自带的反射获得Method测试,仅进行一次method获取

@Test

public void javaReflectGet() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {

Method getName = SimpleBean.class.getMethod("getName");

Stopwatch watch = Stopwatch.createStarted();

for (long i = 0; i < times; i++) {

getName.invoke(bean);

}

watch.stop();

String result = String.format(formatter, "javaReflectGet", times, watch.elapsed(TimeUnit.MILLISECONDS));

System.out.println(result);

}

//使用Java自带的Property属性获取Method测试,仅进行一次method获取

@Test

public void propertyGet() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, IntrospectionException {

Method method = null;

BeanInfo beanInfo = Introspector.getBeanInfo(SimpleBean.class);

PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {

if (propertyDescriptor.getName().equals("name")) {

method = propertyDescriptor.getReadMethod();

break;

}

}

Stopwatch watch = Stopwatch.createStarted();

for (long i = 0; i < times; i++) {

method.invoke(bean);

}

watch.stop();

String result = String.format(formatter, "propertyGet", times, watch.elapsed(TimeUnit.MILLISECONDS));

System.out.println(result);

}

//BeanUtils的getProperty测试

@Test

public void beanUtilsGet() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {

Stopwatch watch = Stopwatch.createStarted();

for (long i = 0; i < times; i++) {

BeanUtils.getProperty(bean, "name");

}

watch.stop();

String result = String.format(formatter, "beanUtilsGet", times, watch.elapsed(TimeUnit.MILLISECONDS));

System.out.println(result);

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

3.测试结果

在4核i5-4590@3.30GHz机器上跑以上测试,经过多次测量,基本在以下数值范围附近,测试数据如下:

1. directGet 100000000 times using 37 ms

2. reflectAsmGet 100000000 times using 39 ms

3. javaReflectGet 100000000 times using 222 ms

4. propertyGet 100000000 times using 335 ms

5. beanUtilsGet 100000000 times using 20066 ms

4.结果分析

1.使用reflectAsm库的性能能和直接调用get方法持平

2.Java自带的反射性能大致为直接get的1/6和1/9.

3.BeanUtils的getProperty非常的慢,为直接get性能的1/500,为Java自带反射性能的1/100和1/60.

转载至:https://www.cnblogs.com/Frank-Hao/p/5839096.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值