Performance comparison after using reflection

This may be a silly question, but I'm wondering does it matter whether I've created an object directly or indirectly using reflection, after the object has been made? Reflection is said to be slow, but what if I only use it during the initialization period. Will it make any difference performance wise afterwards? Compared to non-reflection that is.

Comments

  • Reflection is slow. Don't use it, if you are using it million of times in a very short period.

    Object creation is slow. Don't write any "new" statement, if you are using it million of times in a very short period

    String concatenation is slow, don't write any String concatenation code, if you are using it million of times in a very short period.

    Every operation is time consumsing (even if it just consumes 1 ns, it is still time consuming), don't write any code if it is running billion of times in a very short period.

    Conclusion : there are two options :
    1. Don't write any program
    2. Write anything you want as long as the perceived performance is acceptable.
    "Premature optimization is the root of all evil."

    Back to your question :
    Yes, reflection is slower than direct object access, but you may not perceive it, unless you are using it million of times in a very short period (depends on your CPU, short period may mean few seconds or part of a second).

    Read the conclusion again.
  • An object created with reflection will be identical to one created without.
  • Object creation itself can be a slow operation, especially when the object is a big one.
    For example :
        int[]  buffer=new int[100];  // this is a potential performance killer
    

    But we will not try to optimize it unless it is a real performance killer.

    Let's see an example :
    class R
    {
      static int recursion(int depth)
      {
        int[]  buffer=new int[100];  // this is a real performance killer
        if (depth!=0) return recursion(depth-1);
        return depth;
      }
    
      public static void main(String[] args) throws Exception
      {
        long t1=System.currentTimeMillis();
        for (int i=0;i<10000;i++) recursion(1024);
        double elapse=System.currentTimeMillis()-t1;
        elapse/=1000;
        System.out.println("elapse="+elapse);
      }
    }
    

    This program takes 4.453 second to complete in my computer.

    The following optimization would just takes 0.047 second.
    class R
    {
      static int MAX_DEPTH=1024;
      static int[][] preAllocate=new int[MAX_DEPTH+1][100];
    
      static int recursion(int depth)
      {
        int[] buffer=preAllocate[depth];  // don't create object
        if (depth!=0) return recursion(depth-1);
        return depth;
      }
    
      public static void main(String[] args) throws Exception
      {
        long t1=System.currentTimeMillis();
        for (int i=0;i<10000;i++) recursion(1024);
        double elapse=System.currentTimeMillis()-t1;
        elapse/=1000;
        System.out.println("elapse="+elapse);
      }
    
    }
    


    Though it removes the performance killer, whether it worths the effort and complexity still depends on a lot of factors.

    For example, user patience, CPU temperature, multi-threading loading, memory loading...
  • macteki, I believe Mr. Ignorant is asking whether an object created with reflection will perform differently than an object created without reflection after creation. For example:
    Obj obj1 = new Obj();
    Obj obj2 = Class.forName("Obj").newInstance(); // throws
    
    // Is the second method call less efficient? (No.)
    obj1.doSomething();
    obj2.doSomething();
    
  • I have read.

    And what I am answering is : whether an operation is faster or slower is not an issue.

    Just like everyone suggests to use StringBuffer. I still use simple String concatenation in many cases.
  • Four seconds seems like an 'issue' to me.

    I understand what you're saying, but that doesn't mean answering the question isn't important.
  • macteki wrote:
    : whether an operation is faster or slower is not an issue.
    .

    Which is one reason we get sluggish, slow programs.
  • Which is one reason we get sluggish, slow programs.
    ++

    I will sacrifice some performance for code simplicity and speed of development in certain cases, but performance is something I always try and keep in mind.
  • Thank you for all your answers. I know what I need to know now. Indeed, I wasn't referring to the process of object creation itself, but everything afterwards. My question was: after object creation is there any difference - performance wise - when using objects that were created using reflection or not? So the answer is no. This is good to know. I was planning to use reflection to load some modules and plugins during the initialization period only.

    Even though some people talked about something else, it was still quite educative.

    I never knew array allocation was so slow. How do you get around that?

    Also isn't StringBuilder even better than StringBuffer for string concatenation?
  • I never knew array allocation was so slow. How do you get around that?

    Allocate the space you need in advance. Look at the example in macteki's post.
    Also isn't StringBuilder even better than StringBuffer for string concatenation?

    There has been plenty written about this already: search for "stringbuffer stringbuilder" on Google.
Sign In or Register to comment.