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

Ben Phillips Ben_member at pathlink.com
Mon Mar 27 16:57:54 PST 2006


Basically what your benchmark (along with Hong's) prove is that classes and
structs each have different uses (duh!). Use structs in situations like Hong's
case where classes would require a ton of allocation, and use classes in
situations like your case where passing by value would induce enormous overhead
costs.

In article <e09vl7$2u5f$1 at digitaldaemon.com>, Hasan Aljudy says...
>
>Derek Parnell wrote:
>> Classes are slower than structs. Prove otherwise. I'm happy to be shown  
>> that I'm wrong.
>> 
>
>Here's a slightly modified version of Dave's code.
>
>-------
>import std.stdio, std.date;
>
>struct S
>{
>	char[10000] str;
>}
>
>class C
>{
>	char[10000] str;
>}
>
>void main()
>{
>	const int y = 1_000_000_0;
>	
>	//structs
>	{
>		S s;
>		d_time st = getUTCtime();
>		for(int x = 0; x < y; x++)
>		{
>			fooS(s);
>		}
>		d_time en = getUTCtime();
>		writef("struct: ");
>		writefln((en-st) / cast(double)TicksPerSecond);
>	}
>		
>	//classes
>	{
>		C c = new C;
>		d_time st = getUTCtime();
>		for(int x = 0; x < y; x++)
>		{
>			fooC(c);
>		}
>		d_time en = getUTCtime();
>		writef("class: ");
>		writefln((en-st) / cast(double)TicksPerSecond);
>	}
>}
>
>void fooS(S s)
>{
>	s.str[9999] = 15;
>}
>
>void fooC(C c)
>{
>	c.str[9999] = 15;
>}
>----------
>
>on my machine:
>C:\test\d\struct.vs.class\pass>dave
>struct: 44.422
>class: 0.046
>
>that's about 1000 times faster
>
>44.422/.046 = 965.696
>
>Note: I don't believe this to be a valid benchmark, but I believe it's 
>just as valid as Hong's benchmark.
>
>It's designed to make the class version win.
>
>





More information about the Digitalmars-d mailing list