Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Java7 reflection outperforms ReflactASM && MethodHandler
- //java -version
- //java version "1.7.0_25"
- //Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
- //Java HotSpot(TM) Server VM (build 23.25-b01, mixed mode)
- //+ Profiler [Reflection vs MethodHandle count=1000000000]
- //|-- elapsed time [Method] 21.201 seconds.
- //|-- elapsed time [MethodHandle] 68.726 seconds.
- //|-- elapsed time [ReflectASM] 28.492 seconds.
- //|-- elapsed time [Direct] 0.000 seconds.
- //|-- Total [Reflection vs MethodHandle count=1000000000] 118.422 seconds.
- package ru.wintegra;
- import com.esotericsoftware.reflectasm.MethodAccess;
- import org.slf4j.profiler.Profiler;
- import java.lang.invoke.MethodHandle;
- import java.lang.invoke.MethodHandles;
- import java.lang.reflect.Method;
- public class foo {
- public void bar(){
- //System.getProperty("foo");
- }
- public static void main(String[] args) throws Throwable {
- exec(1000000000);
- // exec(10000000);
- // exec(100000);
- //exec(1000);
- }
- private static void exec(final int count) throws Throwable {
- foo obj = new foo();
- Method bar = foo.class.getMethod("bar");
- bar.setAccessible(true);
- MethodHandle methodHandle = MethodHandles.lookup().unreflect(bar);
- MethodAccess methodAccess = MethodAccess.get(foo.class);
- int index = methodAccess.getIndex(bar.getName());
- Profiler profiler = new Profiler("Reflection vs MethodHandle count="+count);
- profiler.start("Method");
- for (int i = 0; i < count; i++) {
- bar.invoke(obj);
- }
- profiler.start("MethodHandle");
- for (int i = 0; i < count; i++) {
- methodHandle.invokeExact(obj);
- }
- profiler.start("ReflectASM");
- for (int i = 0; i < count; i++) {
- methodAccess.invoke(obj, index);
- }
- profiler.start("Direct");
- for (int i = 0; i < count; i++) {
- obj.bar();
- }
- profiler.stop();
- profiler.print();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement