java methodhandler_MethodHandle VS Method, Java反射及MethodHandle性能测试

本文通过一个性能测试示例,比较了Java中直接调用方法、使用MethodHandle以及Method.invoke()的性能差异。结果显示,直接方法调用的性能最佳,而MethodHandle和Method.invoke()的性能相对较慢,尤其是对于静态方法调用,Method.invoke()的性能显著下降。
摘要由CSDN通过智能技术生成

从结果来看, Oracle的Java新特性MethodHandle还有很多要优化的地方

import java.lang.invoke.MethodHandle;

import java.lang.invoke.MethodHandles;

import java.lang.invoke.MethodType;

import java.lang.reflect.Method;

public class InvokePerformanceTest {

public static int sCount;

// static method

public static void increaseStatic() {

sCount++;

}

// instance method

public void increase() {

sCount++;

}

// get the method handler

public MethodHandle getIncreaseHandler(Object obj, String name, boolean isStatic) throws Exception {

MethodType type = MethodType.methodType(void.class);

MethodHandle handler;

if (isStatic) {

handler = MethodHandles.lookup().findStatic(obj.getClass(), name, type);

} else {

handler = MethodHandles.lookup().findVirtual(obj.getClass(), name, type).bindTo(obj);

}

return handler;

}

// get the method

public Method getIncreaseMethod(Object obj, String name, boolean isStatic) throws Exception {

if (isStatic) {

if (obj instanceof Class>) {

return ((Class>) obj).getMethod(name);

} else {

return obj.getClass().getMethod(name);

}

} else {

return obj.getClass().getMethod(name);

}

}

public static void main(String[] args) throws Throwable {

InvokePerformanceTest test = new InvokePerformanceTest();

int count = 100000000; // times

long start, end, gap;

start = System.currentTimeMillis();

for (int i = 0; i < count; i++) {

test.increase();

}

end = System.currentTimeMillis();

gap = end - start;

System.out.println("direct call: " + gap); // call directly

MethodHandle mh = test.getIncreaseHandler(test, "increase", false);

start = System.currentTimeMillis();

for (int i = 0; i < count; i++) {

mh.invoke();

}

end = System.currentTimeMillis();

gap = end - start;

System.out.println("method handler : " + gap); // call method by handler

Method method = test.getIncreaseMethod(test, "increase", false);

start = System.currentTimeMillis();

for (int i = 0; i < count; i++) {

method.invoke(test);

}

end = System.currentTimeMillis();

gap = end - start;

System.out.println("method invoke: " + gap); // call method by invocation

start = System.currentTimeMillis();

for (int i = 0; i < count; i++) {

increaseStatic();

}

end = System.currentTimeMillis();

gap = end - start;

System.out.println("\nstatic direct call: " + gap); // call static method directly

mh = test.getIncreaseHandler(test, "increaseStatic", true);

start = System.currentTimeMillis();

for (int i = 0; i < count; i++) {

mh.invoke();

}

end = System.currentTimeMillis();

gap = end - start;

System.out.println("static method handler : " + gap); // call static method by handler

method = test.getIncreaseMethod(test, "increaseStatic", true);

start = System.currentTimeMillis();

for (int i = 0; i < count; i++) {

method.invoke(null);

}

end = System.currentTimeMillis();

gap = end - start;

System.out.println("static method invoke: " + gap); // call static method by invocation

}

}

结果:

direct call: 7

method handler : 346

method invoke: 231

static direct call: 7

static method handler : 343

static method invoke: 977

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值