struct vs class benchmark (was Re: Give struct the status it deserves)

Derek Parnell derek at psych.ward
Mon Mar 27 04:23:06 PST 2006


On Mon, 27 Mar 2006 22:40:21 +1100, Wolfgang Draxinger  
<wdraxinger at darkstargames.de> wrote:

> Derek Parnell wrote:

>> Classes are slower than structs. Prove otherwise. I'm happy to
>> be shown that I'm wrong.
>
> I don't see a reason for this. There's no difference in accessing
> the data. Put a struct on the stack, the compiler will compile
> it to access it indirectly (i.e. by "pointers"). Allocate a
> class on the heap, it get's accessed in the very same way.

I really do understand the theory. And on the surface, it would seem that  
your analysis is valid, however the timings show otherwise. There really  
is a difference between heap-allocated classes and heap-allocated structs.  
Classes are slower.

Prove it yourself. Write your own benchmark test and submit its results.  
Critique my example - pull it apart and improve it. Show me where I've  
bungled. Until then, the figures show that classes are always slower than  
structs when used for equivalent tasks.

> As a rule of thumb: Any data you going to work with in a D
> program is encapsulated in classes. Stuff that gets dumped into
> files or memory goes into structs. It's that simple.

Yes it is - in theory. Practice may dicate otherwise though.

-- 
Derek Parnell
Melbourne, Australia



More information about the Digitalmars-d mailing list