Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If in doubt, measure it. My results showed no significant difference. When run, the following program produced:</p> <pre><code>7421714 (abstract) 5840702 (interface) 7621523 (abstract) 5929049 (interface) </code></pre> <p>But when I switched the places of the two loops:</p> <pre><code>7887080 (interface) 5573605 (abstract) 7986213 (interface) 5609046 (abstract) </code></pre> <p>It appears that abstract classes are slightly (~6%) faster, but that should not be noticeable; These are nanoseconds. 7887080 nanoseconds are ~7 milliseconds. That makes it a difference of 0.1 millis per 40k invocations (Java version: 1.6.20)</p> <p>Here's the code:</p> <pre><code>public class ClassTest { public static void main(String[] args) { Random random = new Random(); List&lt;Foo&gt; foos = new ArrayList&lt;Foo&gt;(40000); List&lt;Bar&gt; bars = new ArrayList&lt;Bar&gt;(40000); for (int i = 0; i &lt; 40000; i++) { foos.add(random.nextBoolean() ? new Foo1Impl() : new Foo2Impl()); bars.add(random.nextBoolean() ? new Bar1Impl() : new Bar2Impl()); } long start = System.nanoTime(); for (Foo foo : foos) { foo.foo(); } System.out.println(System.nanoTime() - start); start = System.nanoTime(); for (Bar bar : bars) { bar.bar(); } System.out.println(System.nanoTime() - start); } abstract static class Foo { public abstract int foo(); } static interface Bar { int bar(); } static class Foo1Impl extends Foo { @Override public int foo() { int i = 10; i++; return i; } } static class Foo2Impl extends Foo { @Override public int foo() { int i = 10; i++; return i; } } static class Bar1Impl implements Bar { @Override public int bar() { int i = 10; i++; return i; } } static class Bar2Impl implements Bar { @Override public int bar() { int i = 10; i++; return i; } } } </code></pre>
 

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