title: Why is Java reflection invokation faster than method call?
body: <p>I made the discovery and tested it with my application.
  I'm calling and invoking the same method.</p>
  
  <p>I tried different consecutive executions and also different methods (encryption, sorting, etc...) and got the following results.</p>
  
  <pre><code>Executions|Invoke-Call ratio
  
  1         | 62,78%
  
  10        | 107,58%
  
  100       | 76,74%
  
  1000      | 80,01%
  
  10000     | 116,88%
  
  100000    | 82,80%
  
  1000000   | 91,67%
  </code></pre>
  
  <p>I checked if it might be the use of multiple threads but it's not from what I can tell.
  What could be the explanation?</p>
  
  <p>For further clarification an excerpt of my benchmark:
  The invoke part:</p>
  
  <pre><code>Executable executable = new Executable();
  Method execute = executable.getClass().getMethod("execute");
  System.out.println("# Startup finished.");
  for (float i = 0; i &lt;= 6; i++)
  {
      int executions = (int) Math.pow(10, i);
      long start = System.nanoTime();
      for (int j = 0; j &lt;= executions - 1; j++)
      {
          execute.invoke(executable);
      }
      long stop = System.nanoTime();
      long delta = stop - start;
      System.out.println("Invoke;" + executions + ";" + delta);
  }
  System.out.println("# Shutdown finished.");
  </code></pre>
  
  <p>The call part:</p>
  
  <pre><code>Executable executable = new Executable();
  System.out.println("# Startup finished.");
  for (float i = 0; i &lt;= 6; i++)
  {
      int executions = (int) Math.pow(10, i);
      long start = System.nanoTime();
      for (int j = 0; j &lt;= executions - 1; j++)
      {
          executable.execute();
      }
      long stop = System.nanoTime();
      long delta = stop - start;
      System.out.println("Invoke;" + executions + ";" + delta);
  }
  System.out.println("# Shutdown finished.");
  </code></pre>
  
  <p>For this example of Executable class I have taken extra caution to exclude all preparation work from the execute method.</p>
  
  <pre><code>public class Executable
  {
      private int index = 0;
      private int testsize = 1111111;
      private byte[][] plain = new byte[testsize][];
      private byte[][] hashed = new byte[testsize][];
      private SecureRandom securerandom;
      private MessageDigest messagedigest;
  
      public Executable()
      {
          this.securerandom = new SecureRandom();
          this.messagedigest = MessageDigest.getInstance("SHA-256");
          for (int i = 0; i &lt;= testsize - 1; i++)
          {
              this.plain[i] = new byte[8];
              this.securerandom.nextBytes(this.plain[i]);
          }
      }
  
      public void execute()
      {
          messagedigest.update(this.plain[index]);
          this.hashed[index] = messagedigest.digest();
          index++;
      }
  }
  </code></pre>