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

Derek Parnell derek at psych.ward
Sun Mar 26 20:54:58 PST 2006


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.


Here is my amended benchmark test program. In this, I test structs created
on the stack, structs created on the heap, and classes (created on the heap
of course). The class edition runs about 3 times longer than the heap
struct edition and about 16 times longer that the stack struct edition.

The heap struct edition runs about 5 times longer than the stack struct
edition. So given that there is a big difference between classes (on the
heap) and structs on the heap, those differences seem to be attributable to
the implementation of classes over the implementation of structs.

In these tests, all editions do one 'create' per iteration, and all use a
member function in the created object. So in effect, the only differences
are where the object resides and whether its a class or struct.


// -------------- code starts -------------------
private import std.c.stdio;
private import std.c.time;
private import std.stdio;

const static uint n = 100000000;

version(test_struct_stack)
{
    struct Point
    {
    	static int count = 0;
    	int x() { return 1; }
    }

    void doit()
    {
        Point p;
        Point.count += p.x;
    }
    const static char[] Title = "STACK STRUCTS";
}

version(test_struct_heap)
{
    struct Point
    {
    	static int count = 0;
    	int x() { return 1; }
    }

    void doit()
    {
        Point *p = new Point;
        Point.count += p.x;
    }
    const static char[] Title = "HEAP STRUCTS";
}

version(test_class)
{
    class Point
    {
    	static int count = 0;
    	int x() { return 1; }
    }

    void doit()
    {
        Point p = new Point;
        Point.count += p.x;
    }
    const static char[] Title = "CLASSES";
}

int main()
{

	clock_t time1 = clock();
	for (uint i = 0; i < n; ++i)
	{
		doit();
	}

	clock_t time2 = clock();
	double seconds = (time2 - time1);
	seconds /= CLOCKS_PER_SEC;
	int minutes = cast(int)(seconds/60);
	seconds -= minutes*60;

	writefln("Testing %s %d operation took %sm %ss",
	            Title, Point.count, minutes, seconds);
	return 0;

}

// -------------- code ends -------------------

Here is the compile and run output.

---------------------------
C:\temp>build bt -version=test_struct_stack -Tbtss -full
Path and Version : y:\util\build.exe v2.9(1197)
  built on Wed Aug 10 11:03:42 2005
y:\dmd\bin\..\..\dm\bin\link.exe bt,btss.exe,,user32+kernel32,btss.def/noi;

C:\temp>build bt -version=test_struct_heap -Tbtsh -full
Path and Version : y:\util\build.exe v2.9(1197)
  built on Wed Aug 10 11:03:42 2005
y:\dmd\bin\..\..\dm\bin\link.exe bt,btsh.exe,,user32+kernel32,btsh.def/noi;

C:\temp>build bt -version=test_class -Tbtc -full
Path and Version : y:\util\build.exe v2.9(1197)
  built on Wed Aug 10 11:03:42 2005
y:\dmd\bin\..\..\dm\bin\link.exe bt,btc.exe,,user32+kernel32,btc.def/noi;

C:\temp>btss
Testing STACK STRUCTS 100000000 operation took 0m 5.648s

C:\temp>btsh
Testing HEAP STRUCTS 100000000 operation took 0m 27.68s

C:\temp>btc
Testing CLASSES 100000000 operation took 1m 31.462s

C:\temp>
---------------------------

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
27/03/2006 3:29:29 PM



More information about the Digitalmars-d mailing list