title: Does Java JIT cheat when running JDK code?
body: <p>I was benchmarking some code, and I could not get it to run as fast as with <a href="https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html" rel="noreferrer"><code>java.math.BigInteger</code></a>, even when using the exact same algorithm.
  So I copied <a href="https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html" rel="noreferrer"><code>java.math.BigInteger</code></a> source into my own package and tried this:</p>
  
  <pre><code>//import java.math.BigInteger;
  
  public class MultiplyTest {
      public static void main(String[] args) {
          Random r = new Random(1);
          long tm = 0, count = 0,result=0;
          for (int i = 0; i &lt; 400000; i++) {
              int s1 = 400, s2 = 400;
              BigInteger a = new BigInteger(s1 * 8, r), b = new BigInteger(s2 * 8, r);
              long tm1 = System.nanoTime();
              BigInteger c = a.multiply(b);
              if (i &gt; 100000) {
                  tm += System.nanoTime() - tm1;
                  count++;
              }
              result+=c.bitLength();
          }
          System.out.println((tm / count) + "nsec/mul");
          System.out.println(result); 
      }
  }
  </code></pre>
  
  <p>When I run this (jdk 1.8.0_144-b01 on MacOS) it outputs:</p>
  
  <pre><code>12089nsec/mul
  2559044166
  </code></pre>
  
  <p>When I run it with the import line uncommented:</p>
  
  <pre><code>4098nsec/mul
  2559044166
  </code></pre>
  
  <p>It's almost three times as fast when using the JDK version of BigInteger versus my version, even if it's using the exact same code.</p>
  
  <p>I've examined the bytecode with javap, and compared compiler output when running with options:</p>
  
  <pre><code>-Xbatch -XX:-TieredCompilation -XX:+PrintCompilation -XX:+UnlockDiagnosticVMOptions 
  -XX:+PrintInlining -XX:CICompilerCount=1
  </code></pre>
  
  <p>and both versions seem to generate the same code.
  So is hotspot using some precomputed optimisations that I can't use in my code? I always understood that they don't.
  What explains this difference?</p>