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

Hasan Aljudy hasan.aljudy at gmail.com
Mon Mar 27 16:23:05 PST 2006


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