Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,077 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,599 people online right now. Registration is fast and FREE... Join Now!




Create a List<T> at a default size or empty at initialization

 
Reply to this topicStart new topic

Create a List<T> at a default size or empty at initialization

killnine
4 Apr, 2008 - 07:50 AM
Post #1

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 107



Thanked: 3 times
My Contributions
As usual, I just have a question on what people other than myself would do:

I have an array list that is typically populated with anywhere from 100 to 250 string objects. Would you guys typically initialize the arrayList to a size of 250, forgo a minuscule amount of memory, and try and save time when adding elements to the List?

I think that resizing the List is expensive...
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Create A List<T> At A Default Size Or Empty At Initialization
4 Apr, 2008 - 08:19 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Nope, never give the things an inital capacity; they should bloody well be smart enough to get along without that. The only real cost would be speed over memory; I'll take the minor speed hit.

QUOTE(killnine @ 4 Apr, 2008 - 11:50 AM) *

I think that resizing the List is expensive...


Depends how the underlying list is implemented. A reasonable implemention would be some kind of linked list, in which case the expense is just creating and adding nodes, plus extra house keeping, like maybe an object hash index. Regardless of size, you're always going to be creating and adding nodes. If this is the case, then specifing an initial size could be very trivial indeed.

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 07:27PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month