What is the performance penalty, if any, of using the Class.creatInstance and then Method.invoke methods as compared to explicitly instantiating using new and just calling the instance method?

Ryan Breidenbach

Yes, there is a performance cost. An Object (a Class or Method) is created. Plus, the method name must me looked up dynamically. However the performance penalty is not that big. I don't have any benchmarks, but this (somewhat simplistic) code snippet may give you an idea:

import java.lang.reflect.*;

public class ReflectionTest {

  public static void main(String args[]) {
    test(10000);
    System.out.println("");
    test(100000);
    System.out.println("");
    test(1000000);
  }

  private static void test(int loops) {

    Object object = new Object();
    Class c = Object.class;

    long start = System.currentTimeMillis();
    for(int i = 0; i < loops; i++) {
      object.toString();
    }

    System.out.println(loops + " regular method calls:" +
        (System.currentTimeMillis() - start) + " milliseconds.");

    Method method = null;
    try {
      method = c.getMethod("toString", null);

      start = System.currentTimeMillis();
      for(int i = 0; i < loops; i++) {
        method.invoke(object, null);
      }

      System.out.println(loops + " reflective method calls without lookup:" +
          (System.currentTimeMillis() - start) + " milliseconds.");

      start = System.currentTimeMillis();
      for(int i = 0; i < loops; i++) {
        method = c.getMethod("toString", null);
        method.invoke(object, null);
      }

      System.out.println(loops + " reflective method calls with lookup:" +
          (System.currentTimeMillis() - start) + " milliseconds.");
    }
    catch (Exception e) {} // ignore
  }
}

Running this on my machine yielded the following result:

10000 regular method calls:160 milliseconds.
10000 reflective method calls without lookup:171 milliseconds.
10000 reflective method calls with lookup:570 milliseconds.

100000 regular method calls:1402 milliseconds.
100000 reflective method calls without lookup:1823 milliseconds.
100000 reflective method calls with lookup:5548 milliseconds.

1000000 regular method calls:14140 milliseconds.
1000000 reflective method calls without lookup:18277 milliseconds.
1000000 reflective method calls with lookup:55630 milliseconds.

As you can see, there is a performance hit. However, relative to other performance issues - poor design, file I/O, network latency - using reflection does not have that big of a impact. In fact, it is critical in many of Java's most attractive technologies, like JavaBeans and JavaServer Pages.

Also, there are other consequences to keep in mind when using reflection. On the upside, it can provide a great deal of flexibility to your application. On the downside, you lose some static typing that may lead to runtime errors if you aren't careful.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact