473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

List<SomeStructure> mList;

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<StructAmList;
List<ClassAmList;

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

Aug 24 '07 #1
4 1419
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<Tdoes 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<StructAmyList = ...;
StructA myItem = myList[0];
myItem.Property = value;

Compared to doing this:

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

Will have two different effects.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"J2EE Convert" <pi****@gmail.comwrote in message
news:11*********************@q5g2000prf.googlegrou ps.com...
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<StructAmList;
List<ClassAmList;

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

Aug 24 '07 #2

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:O3*************@TK2MSFTNGP05.phx.gbl...
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<Tdoes 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<StructAmyList = ...;
StructA myItem = myList[0];
myItem.Property = value;

Compared to doing this:

List<ClassAmyList = ...;
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]
- mv*@spam.guard.caspershouse.com

"J2EE Convert" <pi****@gmail.comwrote in message
news:11*********************@q5g2000prf.googlegrou ps.com...
>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<StructAmList;
List<ClassAmList;

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

Aug 25 '07 #3

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:uI**************@TK2MSFTNGP06.phx.gbl...
>
<snip>
>
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
Aug 26 '07 #4

"Bill Butler" <qw****@asdf.comwrote in message
news:HR3Ai.28420$Bv1.7966@trnddc06...
>
"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:uI**************@TK2MSFTNGP06.phx.gbl...
>>
<snip>
>>
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.
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.
>
Bill

Aug 27 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have...
9
by: Gary van der Merwe | last post by:
Hi I want to write my own collection object. I want it to have the following features. 1.. It must be strongly typed (to a Class that I have written). 2.. I should be able to add and...
1
by: Jason P Opdycke [MSFT] | last post by:
Hello All - I have 2 list boxes. Items in Left list box populated from a DB. I remove an item from the left box and add an item to the right box to allow user selection. When that item is...
2
by: Joe Fallon | last post by:
I have 2 listboxes on a Web Form. As I move an item from 1 to the other it shows up at the end of the list. How can I sort the list that just got the new item added to it so it is in alphabetical...
7
by: Kenneth H. Young | last post by:
How does one get the values of multiple selected rows in a DataGrid? I am trying to allow the builing of an email list generated from an LDAP query. The below code works OK but it can create...
10
by: Kenneth H. Young | last post by:
I have created an application to query contact data from an LDAP server and programtically create a new email to: contacts that had been highlithed by the user. Now there is a request to add a...
0
by: jeytu | last post by:
Hi all, I'm a newbie with .net. I have created a web method that would return a class. The definition of the class is <Serializable()> Public Class Response Dim l_status As Boolean ...
1
by: mudasserrafiq | last post by:
I am using following asp file default.asp <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <META http-equiv=Content-Type content="text/html; charset=windows-1252"> <META content="0...
23
by: Rotsey | last post by:
Hi, I am writing an app that scans hard drives and logs info about every fine on the drive. The first iteration of my code used a class and a generic list to store the data and rhis took...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.