Performance of method calls

John Demme me at teqdruid.com
Fri Dec 1 02:07:15 PST 2006


Daniel Keep wrote:

> Hi.
> 
> I'm currently working on a research project as part of my Honours, and
> one of the requirements is speed--the code I'm writing has to be very
> efficient.
> 
> Before I started, my supervisor warned me about object-oriented
> programming and that it seems to be much slower than just flat function
> calls.
> 
> Anyway, I was wondering what the difference between three kinds of
> function calls would be:
> 
> 1. Foo x; x.call(); // Where Foo is a struct
> 2. Foo_call(x); // C-style API
> 3. auto y = new FooClass; y.call(); // Where call is final
> 
> I hooked up a test app which used a loop with 100,000 iterations for
> each call type, and ran that program 100 times, and averaged the outputs.
> 
> #1 was 2.84 times slower than #2, and #3 was 3.15 times slower than #2.
>   Are those numbers right??  Is it really that much slower?  I would
> have thought that they should have been about the same since each one
> needs to pass only one thing: a pointer.  I've attached the test
> programs I used; if anyone can offer any insight or even corrections,
> I'd be very grateful.
> 
> Incidentally, any other insights regarding performance differences
> between OO-style and flat procedural-style would be very welcome.
> 
> -- Daniel

D's advantages over C are far more than just OO.  Write equivalent test
programs in C and D and test them with DMC and DMD.  I'd bet that they'll
be about the same.  This being the case, you can use D but avoid OO calls
if they turn out to be too heavy weight.  For a program of any size in C,
I'd kill for features like dynamic arrays, templates, and the like.

Of course, I suspect that even if DMD's OO performance isn't top notch right
now, it will become so.  I've heard Walter mention that he hasn't yet tuned
the backend optimizer for OO code- I don't know if this is still true, but
it's probably a safe bet that DMD's performance a few months from now will
be significantly better than now.

-- 
~John Demme
me at teqdruid.com
http://www.teqdruid.com/



More information about the Digitalmars-d mailing list