6

I have a class that contains a few basic types. (3x float, 2x int).

Now I need a collection that can hold several million instances of this class. I need no derived types. All elements are exactly from this single class. Further more the number of elements is fixed. In very rarely cases I plan to copy the whole list/array and modify the copy. The originally list/array shall be immutable, thus I don't need to syncronize with other threads.

Now the question are:

  • Do I benefit from an Array instead of a List?
  • Do I save memory using an Array?
  • What about speed?

I read that a List in C# is internally implemented as Array as well.

If it would be C++, I know that the array would hold the complete object. But I'm not sure how C# handles this. Will a C# array only hold references to the class instances or will it hold the complete datastructure?

10
  • 1
    In .NET both arrays and lists hold references Commented Oct 22, 2014 at 6:39
  • I think You will find some answers here Commented Oct 22, 2014 at 6:40
  • Regarding the questions about memory and speed, have you compared them yourself? Commented Oct 22, 2014 at 6:42
  • 2
    @Larry Using struct doesn't make your elements immutable by default. You still have to define it in immutable way. Commented Oct 22, 2014 at 6:42
  • You cannot have an immutable array. Anyway, having a struct instead of a class will make your properties immutable inside your object. Commented Oct 22, 2014 at 6:44

2 Answers 2

6

The originally list/array shall be immutable, thus I don't need to synchronize with other threads.

Did you consider an immutable collection instead of T[] or List<T>? ImmutableArray<T> would make the most sense. You can use ImmutableArray<T>.Builder to create the collection in efficient way.

  • Do I benefit from an Array instead of a List?

If you don't need the number of elements to change you should use Array. It will make it clear to everyone who looks at your code that you're not changing the number of elements.

  • Do I save memory using an Array?

It depends how you'd create the List<T>. Internally, when you add elements to List<T> one by one underlying array's size is changes using 2* multiplier: when there is not enough space for new element current underlying array is replaced by a new one with twice the size. So yes, you can save memory using Array directly, because you won't have any unnecessary memory allocated. However, you can achieve the same using List<T>, either by creating it using constructor that takes list capacity or by calling TrimExcess method after all elements are added to the list.

  • What about speed?

Using array you'll save logic that makes List<T> methods, properties and indexer property calls translated to underlying array calls. But you shouldn't care about that, it will be unnoticeable.

If it would be C++, I know that the array would hold the complete object. But I'm not sure how C# handles this. Will a C# array only hold references to the class instances or will it hold the complete datastructure?

It depends. If you define your type as reference type (a class) both array and list would just hold a reference to particular items. If you define it as value type (a struct), array will hold the actual elements.

Sign up to request clarification or add additional context in comments.

3 Comments

Immutable list or array are known to have the performance issues, they are not the best choice, as they would create a whole new copy every time and if elements are in millions that's even bad. Concurrent collections score higher than Immutable list or array, but concurrent collections doesn't have a list or array replacement
If your code consists of simple computations without things like I/O and uses arrays/lists heavily, you can expect an array to be about twice as fast IME. The difference can be larger; if you're CPU bound it's certainly an easy win you might as well take.
@MrinalKamboj Yes, that's true. You can minimize the creation overhead using builder class. But even then accessing that structure will not be as fast as accessing a simple array. Question is what is more important for certain use case - if you need the fastest solution, use an array,
0
  1. Not really. Internally lists are just plain arrays, and additionally counter for elements in List(not in array). You only will have more poor functionality.
  2. If you know count of elements at, and set it while list initialization, then you will not save any memory. Memory from using List usage is very little, and while list size is fixed it's not depend on count of elements. But if you don's specify exact list size, then size of internal array will be doubled every time when you add new element and count of List elements is bigger than internal array size.
  3. No. Most time it's same as working with plain arrays. Except internal array re-sizing times(but it's not too long, and will not happen when List is fixed).

In C++ Arrays and Lists not always store whole complete object inside it. They can contain contain just references. Same thing in .NET languages(true even for managed C++). Value-types will be stored as-is. Reference-types will be stored... as references to objects.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.