Again - interfaces vs. inheritence

F

Fuzzy

The thread on 'why NOT to use interface?' has provoked a question -
but I'll start a new thread....

Is there any performance difference that anyone is aware of between
using interfaces for polymorphism instead of inheritance? I'm aware
of the OOP design plusses and minuses of each approach, but haven't
read much about implementation differences that may affect
performance.

I'm assuming (dangerous I know) that both approaches are implemented
through v-tables and have the same calling overhead. Therefore,
neither approach enables the JIT to inline short methods.

In the time-critical section of the app that I am designing, I don't
have the requirement of multiple interfaces being implemented by a
class, so there is no clear design decision between an interface or an
abstract base class. Also, there MAY BE SOME common code that could
be shared from the base class, but, at this point, probably not enough
to even really care about.

Any insights?
 
M

mikeb

Fuzzy said:
The thread on 'why NOT to use interface?' has provoked a question -
but I'll start a new thread....

Is there any performance difference that anyone is aware of between
using interfaces for polymorphism instead of inheritance? I'm aware
of the OOP design plusses and minuses of each approach, but haven't
read much about implementation differences that may affect
performance.

I'm assuming (dangerous I know) that both approaches are implemented
through v-tables and have the same calling overhead. Therefore,
neither approach enables the JIT to inline short methods.

In the time-critical section of the app that I am designing, I don't
have the requirement of multiple interfaces being implemented by a
class, so there is no clear design decision between an interface or an
abstract base class. Also, there MAY BE SOME common code that could
be shared from the base class, but, at this point, probably not enough
to even really care about.

Any insights?

I recall a talk by Anders Hejlsberg where he indicated that calling an
interface method through an interface reference has a slightly higher
overhead because there is an extra pointer dereference to get to the
interface-specific v-table.

I think this would only be noticeable (or even measurable) in the most
performance sensitive area. I imagine memory access issues would
otherwise override the difference in performance.

The key thing is that interfaces let you have polymorphism between
classes in different branches of the class hierarchy. Since .NET
supports only single inheritance of classes, interfaces are often the
only way to go to get polymorphism.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top