Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is variation on Bozho example. It runs longer and re-uses the same objects so the cache size doesn't matter so much. I also use an array so there is no overhead from the iterator.</p> <pre><code>public static void main(String[] args) { Random random = new Random(); int testLength = 200 * 1000 * 1000; Foo[] foos = new Foo[testLength]; Bar[] bars = new Bar[testLength]; Foo1Impl foo1 = new Foo1Impl(); Foo2Impl foo2 = new Foo2Impl(); Bar1Impl bar1 = new Bar1Impl(); Bar2Impl bar2 = new Bar2Impl(); for (int i = 0; i &lt; testLength; i++) { boolean flip = random.nextBoolean(); foos[i] = flip ? foo1 : foo2; bars[i] = flip ? bar1 : bar2; } long start; start = System.nanoTime(); for (Foo foo : foos) { foo.foo(); } System.out.printf("The average abstract method call was %.1f ns%n", (double) (System.nanoTime() - start) / testLength); start = System.nanoTime(); for (Bar bar : bars) { bar.bar(); } System.out.printf("The average interface method call was %.1f ns%n", (double) (System.nanoTime() - start) / testLength); } </code></pre> <p>prints</p> <pre><code>The average abstract method call was 4.2 ns The average interface method call was 4.1 ns </code></pre> <p>if you swap the order the tests are run you get</p> <pre><code>The average interface method call was 4.2 ns The average abstract method call was 4.1 ns </code></pre> <p>There is more difference in how you run the test than which one you chose.</p> <p>I got the same result with Java 6 update 26 and OpenJDK 7.</p> <hr> <p>BTW: If you add a loop which only call the same object each time, you get</p> <pre><code>The direct method call was 2.2 ns </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload