Advertisement
Guest User

java7 reflection benchmark

a guest
Mar 5th, 2014
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. // Java7 reflection outperforms ReflactASM && MethodHandler
  2. //java -version
  3. //java version "1.7.0_25"
  4. //Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
  5. //Java HotSpot(TM) Server VM (build 23.25-b01, mixed mode)
  6. //+ Profiler [Reflection vs MethodHandle count=1000000000]
  7. //|-- elapsed time                 [Method]    21.201  seconds.
  8. //|-- elapsed time           [MethodHandle]    68.726  seconds.
  9. //|-- elapsed time             [ReflectASM]    28.492  seconds.
  10. //|-- elapsed time                 [Direct]     0.000  seconds.
  11. //|-- Total        [Reflection vs MethodHandle count=1000000000]   118.422  seconds.
  12.  
  13. package ru.wintegra;
  14.  
  15. import com.esotericsoftware.reflectasm.MethodAccess;
  16. import org.slf4j.profiler.Profiler;
  17.  
  18. import java.lang.invoke.MethodHandle;
  19. import java.lang.invoke.MethodHandles;
  20. import java.lang.reflect.Method;
  21.  
  22. public class foo {
  23.     public  void bar(){
  24.         //System.getProperty("foo");
  25.     }
  26.     public static void main(String[] args) throws Throwable {
  27.         exec(1000000000);
  28. //        exec(10000000);
  29. //        exec(100000);
  30.         //exec(1000);
  31.     }
  32.  
  33.     private static void exec(final int count) throws Throwable {
  34.         foo obj = new foo();
  35.         Method bar = foo.class.getMethod("bar");
  36.         bar.setAccessible(true);
  37.         MethodHandle methodHandle = MethodHandles.lookup().unreflect(bar);
  38.         MethodAccess methodAccess = MethodAccess.get(foo.class);
  39.         int index = methodAccess.getIndex(bar.getName());
  40.  
  41.         Profiler profiler = new Profiler("Reflection vs MethodHandle count="+count);
  42.  
  43.         profiler.start("Method");
  44.         for (int i = 0; i < count; i++) {
  45.             bar.invoke(obj);
  46.         }
  47.  
  48.         profiler.start("MethodHandle");
  49.         for (int i = 0; i < count; i++) {
  50.             methodHandle.invokeExact(obj);
  51.         }
  52.  
  53.         profiler.start("ReflectASM");
  54.         for (int i = 0; i < count; i++) {
  55.             methodAccess.invoke(obj, index);
  56.         }
  57.  
  58.         profiler.start("Direct");
  59.         for (int i = 0; i < count; i++) {
  60.             obj.bar();
  61.         }
  62.  
  63.  
  64.         profiler.stop();
  65.         profiler.print();
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement