views:

114

answers:

5

If I have an object with 50 getters/setters, where every 10 of them is defined under a new interface, and I type the object as one of those interfaces, will it increase performance?

Not sure how method/variable lookups work, but it seems that if you type the object to something that has fewer methods, it would be faster to run those methods?

I'm asking for Actionscript/Java, but this applies to any language I guess.

So if I had an object, DisplayObject, that implemented 5 interfaces (IResizable, IScalable, IMovable, IMeasurable, IDrawable), and I typed it as IResizable to access the x and y accessors, is that an optimization?

Otherwise it seems the only use for interfaces is to make your code more readable, and to make sure you stick to defined patterns.

+4  A: 

If the code is running natively (jitted/compiled), interface methods amount to virtual calls. A class will have a v-table that lists the addresses of fuctions for each interface method. This adds a tiny bit of indirection, but is generally very fast. This is the same way that virtual methods work. They will usually be equal or very close in execution speed.

With non-virtual methods (many languages require virtual methods to be explicitly declared as so), the address is hard-coded and slightly faster.

There is no "searching for a method" (this is sorted out at compile time), so the number of methods is irrelevant.

Of course, this is a generalized answer and does not at all apply to interpreted code.

Snarfblam
+1  A: 

is that an optimization?

By casting to an interface, you're reducing the number of methods which are supported by that method. That might make it faster, if the cost to invoke a method is proportional to the number of methods, however I don't know any languages where that's true: so my answer is, "I think not."

Otherwise it seems the only use for interfaces is to make your code more readable, and to make sure you stick to defined patterns.

The main reason for interfaces is IMO as follows: if for example I implement a DeviceManager class like this ...

interface IDevice
{
  string name { get; }
  string description { get; }
  bool start();
  bool stop();
}

class DeviceManager
{
  void install(IDevice device) { ... }
  void displayUI(Windows window) { ... }
}

... then I can write a GUI to manage any kind of device that implements the IDevice interface: so long as their class implements the IDevice interface, it can even manage device instances whose class hadn't yet been written at the time when I wrote DeviceManager.

ChrisW
A: 

For java, because the method call of an object is decided during run-time, it really doesn't matter. An interface is not just for readable, it is used to make sure the object hierarchy and polymorphism.

Henry Gao
+3  A: 

I once benchmarked a Java program, and found that interface method calls are twice as slow as class method calls. So there is some extra overhead (perhaps another indirect pointer dereference) for interface calls.

But unless you're really tight on execution time (e.g., real-time audio or graphics processing), I'm not sure the extra overhead is worth worrying about.

Loadmaster
A: 

Write a test program, in your environment which uses both these methods. Have each method be invoked 1000000 times and measure which is faster.

After you do that, add a tiny bit of work to each function (you know, do a square root or something). Watch the relevance of your previous test fade into the noise

Tom Leys