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

Hasan Aljudy hasan.aljudy at gmail.com
Mon Mar 27 12:52:05 PST 2006


Derek Parnell wrote:
> On Mon, 27 Mar 2006 17:12:34 +1100, Hasan Aljudy 
> <hasan.aljudy at gmail.com> wrote:
<snip>
>>
>> The program doesn't really do anything, you know.
> 
> Wow! Really?! Are you sure?

Yes, I'm sure.

In practical situations, allocating stuff on the heap is not really what 
you want to do in your program; it's just a way to achieve what you want 
to do.

If you don't understand what I mean, take this very simple example:
You want to print numbers 1 through 1000 on the screen.

you can do it with a while loop:

	int i = 0;
	while( i <= 1000 )
	{
		writefln(i);
		i++;
	}

but, you can also do it with a for loop:

	for( int i = 0; i <= 1000; i++ )
	{
		writefln(i);
	}

and a goto

	int i = 0;
	
	doit:
	i++;
	writefln(i);
	if( i <= 1000 ) goto doit;

and here's another while loop:

	int i = 0;
	while( true )
	{
		writefln(i);
		i++;
		if( i > 1000 ) break;
	}

or, you can do it sorta like this:

	writefln(1);
	writefln(2);
	writefln(3);
	.
	.
	.
	writefln(999);
	writefln(1000);

all these programs do the same thing, they just do it differently.

Philisophically, you can argue that these programs do different things, 
the first one does a while loop, the second one does a for loop, the 
third one does a goto, the last one does it sequentially .. etc.

However, practically, you're doing the same thing, in different ways.
Some ways can be faster than the others.

Here for example, is a stupid way of doing the above program:

	for( int i = 0; i <= 1000; i++ )
	{
		writefln(i);
		for( int j = 0; j < 1000000000; j++ )
		{
			//waste time
			int y = 10;
			int x = 5;
			x = x + y;
		}
	}

However, it still works. Just .... very slow.


This applies to the given benchmark.
You're not doing anything, besides wasting heap space and wasting time 
allocating on the heap.

> 
>> 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.
> 
> I think you are deliberately missing the point, Hasan. I thought the 
> point was to show that heap-allocated things are inherently slower than 
> stack-allocated things. The original example didn't do a good job of 
> that but my subsequent example does I believe. Can you please comment on 
> my example code?

OK, sorry, I didn't check your example yet. This does not affect my point.

Memory allocation is not the only thing you do with classes/structs. I'd 
like to think of it as just an implementation/optimization issue.

One thing were classes would win over structs is pass by reference vs. 
pass by value.

Create a huge struct, with say, 40 or 50 fields, instantiate it once, 
then pass it around by value inside a loop (say, a 1000 times).
Then edit the program and change the struct to a class, and when you 
create it, use "new", and don't change anything else.


>> 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.
> 
> But I think my example does show that classes (allocated on the heap) 
> are slower than structs whether allocated on the heap or on the stack.
> 
> Classes are slower than structs. Prove otherwise. I'm happy to be shown 
> that I'm wrong.
> 
> --Derek Parnell
> Melbourne, Australia



More information about the Digitalmars-d mailing list