java methodhandle_java – MethodHandle性能

我写了一个基准测试来测试java.lang.invoke.MethodHandle,java.lang.reflect.Method和方法的直接调用的性能。

我看到MethodHandle.invoke()性能几乎和直接调用相同。但是我的测试结果显示另一个:MethodHandle调用比反射慢约三倍。我的问题是什么?可能这是JIT优化的结果吗?

public class Main {

public static final int COUNT = 100000000;

static TestInstance test = new TestInstance();

static void testInvokeDynamic() throws NoSuchMethodException, IllegalAccessException {

int [] ar = new int[COUNT];

MethodHandles.Lookup lookup = MethodHandles.lookup();

MethodType mt = MethodType.methodType(int.class);

MethodHandle handle = lookup.findStatic(TestInstance.class, "publicStaticMethod", mt) ;

try {

long start = System.currentTimeMillis();

for (int i=0; i

ar[i] = (int)handle.invokeExact();

}

long stop = System.currentTimeMillis();

System.out.println(ar);

System.out.println("InvokeDynamic time: " + (stop - start));

} catch (Throwable throwable) {

throwable.printStackTrace();

}

}

static void testDirect() {

int [] ar = new int[COUNT];

try {

long start = System.currentTimeMillis();

for (int i=0; i

ar[i] = TestInstance.publicStaticMethod();

}

long stop = System.currentTimeMillis();

System.out.println(ar);

System.out.println("Direct call time: " + (stop - start));

} catch (Throwable throwable) {

throwable.printStackTrace();

}

}

static void testReflection() throws NoSuchMethodException {

int [] ar = new int[COUNT];

Method method = test.getClass().getMethod("publicStaticMethod");

try {

long start = System.currentTimeMillis();

for (int i=0; i

ar[i] = (int)method.invoke(test);

}

long stop = System.currentTimeMillis();

System.out.println(ar);

System.out.println("Reflection time: " + (stop - start));

} catch (Throwable throwable) {

throwable.printStackTrace();

}

}

static void testReflectionAccessible() throws NoSuchMethodException {

int [] ar = new int[COUNT];

Method method = test.getClass().getMethod("publicStaticMethod");

method.setAccessible(true);

try {

long start = System.currentTimeMillis();

for (int i=0; i

ar[i] = (int)method.invoke(test);

}

long stop = System.currentTimeMillis();

System.out.println(ar);

System.out.println("Reflection accessible time: " + (stop - start));

} catch (Throwable throwable) {

throwable.printStackTrace();

}

}

public static void main(String ... args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InterruptedException {

Thread.sleep(5000);

Main.testDirect();

Main.testInvokeDynamic();

Main.testReflection();

Main.testReflectionAccessible();

System.out.println("\n___\n");

System.gc();

System.gc();

Main.testDirect();

Main.testInvokeDynamic();

Main.testReflection();

Main.testReflectionAccessible();

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值