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

Hong Hong_member at pathlink.com
Sun Mar 26 20:54:15 PST 2006


Probably should point out that the major point of the benchmark is to test the
allocation and deallocation of 100000000 object or struct, i.e. test the
performance between "stack allocation" and "heap allocation with GC".

Anyway, your modified version is very flawed, for a number of reasons,

* only one allocation is done for the class version but 100000000 structs were
created, clearly not a comparison here.

* structs are more or less designed to use stack allocation, but you forced it
to use heap allocation and made it GC collected. This does not demonstrate how
structs are used 99% of the time.

In article <e07ka1$2kml$1 at digitaldaemon.com>, Hasan Aljudy says...
>
>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.
>
>on my machine:
> >class
>100000000
>operation took 0 m 0.25 s
>
> >struct
>100000000
>operation took 0 m 15.265 s
>
>
>Try to come up with a more realistic example, and optamize both 
>versions. This way, you can prove that it's the class vs. struct issue 
>and not an optimization issue.
>
>Hong wrote:
>> Ok, even people are telling me that class is not slower, I have decided to make
>> a benchmark to test them out. Here they are,
>> 
>> Version using struct
>> http://members.iinet.net.au/~honglee/benchmark/benchmarkstruct.d
>> 
>> Version using class
>> http://members.iinet.net.au/~honglee/benchmark/benchmarkclass.d
>> 
>> On my computer, the struct version runs 300 times faster than the class version.
>> Interestingly, deleting the object explicitly in the class version actually
>> slowed the program even further.
>> 
>> The difference seems real.
>> 
>> In article <e07506$1ss2$1 at digitaldaemon.com>, Hasan Aljudy says...
>> 
>>>What's wrong with classes?!
>>>
>>>Classes won't make your code slow.
>>>
>> 
>> 
>> 
>





More information about the Digitalmars-d mailing list