title: newInstance vs new in jdk-9/jdk-8 and jmh
body: <p>I've seen a lot of threads here that compare and try to answer which is faster: <code>newInstance</code> or <code>new operator</code>.</p>
  
  <p>Looking at the source code, it would seem that <code>newInstance</code> should be <strong>much slower</strong>, I mean it does so many security checks and uses reflection. And I've decided to measure, first running jdk-8. Here is the code using <code>jmh</code>.</p>
  
  <pre><code>@BenchmarkMode(value = { Mode.AverageTime, Mode.SingleShotTime })
  @Warmup(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS)   
  @Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS)    
  @State(Scope.Benchmark) 
  public class TestNewObject {
      public static void main(String[] args) throws RunnerException {
  
          Options opt = new OptionsBuilder().include(TestNewObject.class.getSimpleName()).build();
          new Runner(opt).run();
      }
  
      @Fork(1)
      @Benchmark
      public Something newOperator() {
         return new Something();
      }
  
      @SuppressWarnings("deprecation")
      @Fork(1)
      @Benchmark
      public Something newInstance() throws InstantiationException, IllegalAccessException {
           return Something.class.newInstance();
      }
  
      static class Something {
  
      } 
  }
  </code></pre>
  
  <p>I don't think there are big surprises here (JIT does a lot of optimizations that make this difference not <em>that big</em>):</p>
  
  <pre><code>Benchmark                  Mode  Cnt      Score      Error  Units
  TestNewObject.newInstance  avgt    5      7.762 ±    0.745  ns/op
  TestNewObject.newOperator  avgt    5      4.714 ±    1.480  ns/op
  TestNewObject.newInstance    ss    5  10666.200 ± 4261.855  ns/op
  TestNewObject.newOperator    ss    5   1522.800 ± 2558.524  ns/op
  </code></pre>
  
  <p>The difference for the hot code would be around <strong>2x</strong> and much worse for single shot time.</p>
  
  <p>Now I switch to jdk-9 (build 157 in case it matters) and run the same code.
  And the results:</p>
  
  <pre><code> Benchmark                  Mode  Cnt      Score      Error  Units
   TestNewObject.newInstance  avgt    5    314.307 ±   55.054  ns/op
   TestNewObject.newOperator  avgt    5      4.602 ±    1.084  ns/op
   TestNewObject.newInstance    ss    5  10798.400 ± 5090.458  ns/op
   TestNewObject.newOperator    ss    5   3269.800 ± 4545.827  ns/op
  </code></pre>
  
  <p>That's a <strong>whooping 50x</strong> difference in hot code. I'm using latest jmh version (1.19.SNAPSHOT).</p>
  
  <p>After adding one more method to the test:</p>
  
  <pre><code>@Fork(1)
  @Benchmark
  public Something newInstanceJDK9() throws Exception {
      return Something.class.getDeclaredConstructor().newInstance();
  }
  </code></pre>
  
  <p>Here are the overall results n jdk-9:</p>
  
  <pre><code>TestNewObject.newInstance      avgt    5    308.342 ±   107.563  ns/op
  TestNewObject.newInstanceJDK9  avgt    5     50.659 ±     7.964  ns/op
  TestNewObject.newOperator      avgt    5      4.554 ±     0.616  ns/op    
  </code></pre>
  
  <p>Can someone shed some light on <em>why there is such a big difference</em>?</p>