List<SomeStructure> mList;

J

J2EE Convert

Hello,

Sorry if this has already been beaten to death. However, I was not
able to find the answer in my searches. Is there any performance trade
off between creating and using a generic list of a Class Type and a
generic list of a Struct Type assuming both types are the same save
for one being a structure and one being a class. See below.

List<StructA> mList;
List<ClassA> mList;

Assuming that ClassA and StructA are exactly the same see below.

//Each type has 2 strings and a getter property for each string.
string mFirstName;
string mLastName;


My initial thought was this is a good use for a structure however I am
not sure what the implications of adding such a structure to a generic
list is. Is there any overhead with moving the structure from the
stack to heap since it is now contained within a reference type etc..

I know it may be a crappy example but I am trying to get my head
around it.

PD
 
N

Nicholas Paldino [.NET/C# MVP]

PD,

When you have:

List<StructA>
List<ClassA>

In a method, the references are the only thing on the stack. They both
point to objects on the heap. The internal storage for the items are array
references (which are on the heap as well) to either structures or classes.

There is really little difference in this case. There should be a
little overhead in using the generic list with a structure, which is because
you are copying the value from the list every time you are assigning it
(value types are copied when assigned, and passed as parameters, unless
passed by ref, which List<T> does not do).

If you want to see just how much of a performance difference there is,
then it's easy enough to test (use the Stopwatch class in the
System.Diagnostics namespace).

However, I would be more concerned with the semantics of using a
structure versus the performance implications, as doing this:

List<StructA> myList = ...;
StructA myItem = myList[0];
myItem.Property = value;

Compared to doing this:

List<ClassA> myList = ...;
ClassA myItem = myList[0];
myItem.Property = value;

Will have two different effects.
 
B

Ben Voigt [C++ MVP]

Nicholas Paldino said:
PD,

When you have:

List<StructA>
List<ClassA>

In a method, the references are the only thing on the stack. They both
point to objects on the heap. The internal storage for the items are
array references (which are on the heap as well) to either structures or
classes.

There is really little difference in this case. There should be a
little overhead in using the generic list with a structure, which is
because you are copying the value from the list every time you are
assigning it (value types are copied when assigned, and passed as
parameters, unless passed by ref, which List<T> does not do).

But access to the members of a value type is faster than access to members
of a reference type, and with structures the garbage collector has a lot
less work to do. So for a small type (two strings inside is a small type),
structures will probably be faster.
If you want to see just how much of a performance difference there is,
then it's easy enough to test (use the Stopwatch class in the
System.Diagnostics namespace).

However, I would be more concerned with the semantics of using a
structure versus the performance implications, as doing this:

List<StructA> myList = ...;
StructA myItem = myList[0];
myItem.Property = value;

Compared to doing this:

List<ClassA> myList = ...;
ClassA myItem = myList[0];
myItem.Property = value;

Will have two different effects.

In the end, I would say it depends on whether you can guarantee having the
same instance for equal content. If yes (flyweight pattern), then go with a
reference. If you would need to override the Equals method anyway to do
content comparison, then go with a structure and make it immutable by not
providing property setters, just a public constructor that initializes all
properties.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)



J2EE Convert said:
Hello,

Sorry if this has already been beaten to death. However, I was not
able to find the answer in my searches. Is there any performance trade
off between creating and using a generic list of a Class Type and a
generic list of a Struct Type assuming both types are the same save
for one being a structure and one being a class. See below.

List<StructA> mList;
List<ClassA> mList;

Assuming that ClassA and StructA are exactly the same see below.

//Each type has 2 strings and a getter property for each string.
string mFirstName;
string mLastName;


My initial thought was this is a good use for a structure however I am
not sure what the implications of adding such a structure to a generic
list is. Is there any overhead with moving the structure from the
stack to heap since it is now contained within a reference type etc..

I know it may be a crappy example but I am trying to get my head
around it.

PD
 
B

Bill Butler

But access to the members of a value type is faster than access to
members of a reference type, ...
<snip>

Can you clarify this point?
Perhaps I am being thick, but it seems that the opposite would be true.

Bill
 
B

Ben Voigt [C++ MVP]

Bill Butler said:
<snip>

Can you clarify this point?
Perhaps I am being thick, but it seems that the opposite would be true.

If the value type is copied to the local stack, it is accessed using the ESP
or EBP pointer and it certainly already in cache. If the value type is
accessed in an array, there is good locality of reference and the CPU cache
functions optimally.

On the other hand, accessing members of a reference type uses up a general
use register to hold the handle and then accesses memory scattered through
the heap, which may have very bad cache behavior.

This may or may not be as important as the cost of adjusting so many
tracking handles when the GC runs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top