Guest User

Untitled

a guest
Jun 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. public class Main {
  2.  
  3. static class A {
  4. public Integer a() { return 1; }
  5. }
  6.  
  7. public static void main(String[] args) throws Exception {
  8. doRegular();
  9. doReflection2();
  10. doReflection();
  11. }
  12.  
  13. public static void doRegular() throws Exception {
  14. A a = new A();
  15. long start = System.currentTimeMillis();
  16. for (int i=0; i<10000000; i++) {
  17. a.a();
  18. }
  19. System.out.println(System.currentTimeMillis() - start);
  20. }
  21.  
  22. public static void doReflection() throws Exception {
  23. A a = new A();
  24. long start = System.currentTimeMillis();
  25. for (int i=0; i<10000000; i++) {
  26. A.class.getMethod("a").invoke(a);
  27. }
  28. System.out.println(System.currentTimeMillis() - start);
  29. }
  30.  
  31. public static void doReflection2() throws Exception {
  32. A a = new A();
  33. java.lang.reflect.Method method = A.class.getMethod("a");
  34. long start = System.currentTimeMillis();
  35. for (int i=0; i<10000000; i++) {
  36. method.invoke(a);
  37. }
  38. System.out.println(System.currentTimeMillis() - start);
  39. }
  40. }
Add Comment
Please, Sign In to add comment