struct vs class benchmark (was Re: Give struct the status it deserves)
Hong
Hong_member at pathlink.com
Mon Mar 27 04:06:29 PST 2006
Derek made the benchmark quite nice, thank you for that.
Wolfgang wrote:
>The only thing that makes things slow is the allocation of heap
>memory which may cause the garbage collector to kick in.
Wolfgang wrote:
> But
>once the memory is allocated it can be used as fast as stack
>memory.
This is not entirely correct. Memory fragmentation means that cache miss is much
more frequent on heap allocated data structures. Struct makes it such that data
can be bundled nicely together in memory, making them faster to access.
Wolfgang wrote:
>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.
Derek meant that struct allocation is much faster than class allocation (~150x),
allocation needs to be included when measuring performance, not just access
speed.
Wolfgang wrote:
>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.
This is the minimal struct approach. However, since that we have already shown
that struct allocation/deallocation is more than 100x faster, it makes good
sense to use struct for data types that require enormous amount of
allocation/deallocation, such as Vector3d in computer graphics, or Point in GUI
programming. In fact minTL uses struct to create containers.
More information about the Digitalmars-d
mailing list