struct vs class benchmark (was Re: Give struct the status it deserves)
Hasan Aljudy
hasan.aljudy at gmail.com
Sun Mar 26 22:12:34 PST 2006
Derek Parnell wrote:
> On Sun, 26 Mar 2006 19:57:08 -0700, Hasan Aljudy wrote:
>
>
>>Heh, this is so flawed.
>>
>>it's got nothing to do with classes vs. structs.
>>
>>The benchmark is designed so that the struct version runs faster than
>>the class version.
>>
>>change the class version to:
>>
>> Point p = new Point();
>> for (uint i = 0; i < n; ++i)
>> {
>> Point.count += p.x;
>> // delete p;
>> }
>>
>>and the struct version to:
>>
>> for (uint i = 0; i < n; ++i)
>> {
>> Point * p = new Point();
>> Point.count += p.x;
>> }
>>
>>These programs don't logically do anything different. However, the class
>>version now runs much much faster than the struct version.
>
>
> I think that you're amendment does not make them logically or otherwise the
> same. Your class version does one GC operation (create a new object) but
> your struct version does one GC operation per iteration - a new Point is
> allocated for every iteration.
>
The program doesn't really do anything, you know. Creating a new
instance serves no purpose at all. So, as an optimization, you can do
away without constant allocation; just reuse the same object.
The example serves very well to illustrate how slow it is to allocate
millions of objects on the heap in one go.
However, it provides no proof that, in pratical situations, structs are
much much faster than classes. Which is basically what the example tries
to hint at.
More information about the Digitalmars-d
mailing list