Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Performance / January 2007

Tip: Looking for answers? Try searching our database.

Generic collections vs. arrays on structs vs. class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
buu - 13 Dec 2006 19:32 GMT
I started working on a performance demanding app. I created structures that
I put into an Array. Then I realized that arrays are byref objects, while
struct is byval.
Now, I presumed, that managing these objects would be faster if I would use
class instead of structure, cause I can send a reference, not the whole
object as an procedure parameter. Am I right?

but.... I realized that it would be very good to use generic collections
instead of array (that keeps classes). Would it be faster than structs on
arrays? (or even classes put in arrays).

Also, wich kind of generic collections would be the best for it?
These objects have one primary key (because of it Dictionary is one
possibility), but that object has 2-3 index keys wich are often used, but
they're not unique.
Wich way you recomend?
Henning Krause [MVP - Exchange] - 18 Dec 2006 12:14 GMT
Hello,

generic classes (List<T> for example) internally use an array of type T, so
access is relative fast. But List<T> (as well as Dictionary<TKey, TValue>
are not extensible (no virtual methods).

Your option here is either to create a decorator for Dictionary<TKey,
TValue> which implements the Add, Remove methods and handles those
additional indizes.

The other method is to inherit from KeyedCollection. This class has some
extension points which allow you to maintain your special indexes.

If you stick with your array of structs, you end up having one large object
in memory. If this object grows beyond ~85kbytes, it will end up on the
LargeObjectHeap, which will never be defragmented and only garbage-collected
when a full garbage collection is performed (which hopefully doesn't happen
very often).

So, I would go for a collection of classes.

Best regards,
Henning Krause

>I started working on a performance demanding app. I created structures that
>I put into an Array. Then I realized that arrays are byref objects, while
[quoted text clipped - 12 lines]
> they're not unique.
> Wich way you recomend?
Kevien Lee - 20 Dec 2006 09:05 GMT
Hi buu,
may be this article help for you:
http://blogs.msdn.com/ricom/archive/2006/03/09/548097.aspx

> I started working on a performance demanding app. I created structures that
> I put into an Array. Then I realized that arrays are byref objects, while
[quoted text clipped - 12 lines]
> they're not unique.
> Wich way you recomend?
Ben Voigt - 29 Dec 2006 15:20 GMT
>I started working on a performance demanding app. I created structures that
>I put into an Array. Then I realized that arrays are byref objects, while
[quoted text clipped - 6 lines]
> instead of array (that keeps classes). Would it be faster than structs on
> arrays? (or even classes put in arrays).

It depends on whether you ever resize the array, use random or sequential
access, etc.

It's really hard to beat an array of structs for fixed-size random access.
For one thing, it's a single large memory area passed by reference.  As
others have said, it will probably end up on the Large Object Heap which
means the garbage collector can pretty much ignore it.  Another thing is
that since it is stored sequentially, locality of reference is extremely
good so you get maximum benefit from your CPU cache.

You can of course still pass individual elements by reference, either using
the ref keyword, or by passing the array and an index.

> Also, wich kind of generic collections would be the best for it?
> These objects have one primary key (because of it Dictionary is one
> possibility), but that object has 2-3 index keys wich are often used, but
> they're not unique.
> Wich way you recomend?

Does the primary key change?  (Often, Rarely, Never)  If never, then you can
fill the array in sorted order and use the extremely fast+scalable binary
search algorithm.  Again, if objects don't move around, then an index
(meaning offset) is as good as a tracking reference, but without the GC
overhead, so you could store indexes (meaning offset) in your index (meaning
key) structures.

If you add, remove, and move elements frequently, then a Dictionary of
instances will be far better than an array.
Nip - 29 Jan 2007 17:26 GMT
Hi

I have lately written an app to measure exactly this.
I came to the result, that an array of classes is the fastest, but structs
are much less memory consuming.
About the idea of generic collections: their performance is extremely bad (i
got a 4x better performance with arrays).

Signature

Yours,
Lukas Cavigelli
http://www.cavigelli.net

>I started working on a performance demanding app. I created structures that
>I put into an Array. Then I realized that arrays are byref objects, while
[quoted text clipped - 12 lines]
> they're not unique.
> Wich way you recomend?
Nip - 29 Jan 2007 17:58 GMT
I have forgotten to say something that might be important as well:
Memory consumption and performance depends on the language you are using.
Although c# and vb are both .net languages vb needs more memory (and
therefore has to read and write more) because of its compatibility to vb6.

Yours,
Lukas Cavigelli
http://www.cavigelli.net

> Hi
>
[quoted text clipped - 20 lines]
>> they're not unique.
>> Wich way you recomend?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.