Performance of method calls

Daniel Keep daniel.keep+lists at gmail.com
Fri Dec 1 06:04:33 PST 2006


John Demme wrote:
> 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.

Damn straight.  Very first thing I wrote involved templated structs, 
simply because it allowed me to write much cleaner, safer code.  I now 
have a literate library module with contracts and unit tests.

> 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.

Lamentably, I can't go back to my supervisor and say "but this guy said 
that Walter said that it'll totally get faster!"

Incidentally, I wrote up a much larger test program, compiled with about 
five different sets of compiler switches and then disassembled the whole 
shtick and started reading through it.

It's really annoying to write test applications and then discover the 
compiler's elided half of it :P  Damn optimisations...  at least it 
explains the performance increases!

I've decided that I'll try to stick to structures with member functions 
wherever possible.  The main difference in performance appears to be 
creation and destruction of classes vs. structs.  I still want to get 
the newest DMD and see what difference stack allocation makes (silly me 
for doing this without checking to make sure I had the newest compiler...)

The only thing I really miss when using structures are constructors and 
destructors.  Any reason why we can't have them, Walter?  Pwetty pweese? 
  You can make it my Christmas Pressy...

	-- Daniel



More information about the Digitalmars-d mailing list