How and when to abandon the use of arrays in C#?
Asked Answered
T

14

25

I've always been told that adding an element to an array happens like this:

An empty copy of the array+1element is created and then the data from the original array is copied into it then the new data for the new element is then loaded

If this is true, then using an array within a scenario that requires a lot of element activity is contra-indicated due to memory and CPU utilization, correct?

If that is the case, shouldn't you try to avoid using an array as much as possible when you will be adding a lot of elements? Should you use iStringMap instead? If so, what happens if you need more than two dimensions AND need to add a lot of element additions. Do you just take the performance hit or is there something else that should be used?

Testimony answered 16/9, 2008 at 19:24 Comment(0)
D
26

Look at the generic List<T> as a replacement for arrays. They support most of the same things arrays do, including allocating an initial storage size if you want.

Derouen answered 16/9, 2008 at 19:27 Comment(1)
If designing re-usable or framework-like components, consider using the base classes or interfaces, ICollection<T> or IEnumerable<T>. Static code analysis should tell you about this and link to better information (it's for inheritance and such).Back
S
13

This really depends on what you mean by "add."

If you mean:

T[] array;
int i;
T value;
...
if (i >= 0 && i <= array.Length)
    array[i] = value;

Then, no, this does not create a new array, and is in-fact the fastest way to alter any kind of IList in .NET.

If, however, you're using something like ArrayList, List, Collection, etc. then calling the "Add" method may create a new array -- but they are smart about it, they don't just resize by 1 element, they grow geometrically, so if you're adding lots of values only every once in a while will it have to allocate a new array. Even then, you can use the "Capacity" property to force it to grow before hand, if you know how many elements you're adding (list.Capacity += numberOfAddedElements)

Somali answered 16/9, 2008 at 19:34 Comment(1)
Good call on the capacity valueGherardi
S
5

In general, I prefer to avoid array usage. Just use List<T>. It uses a dynamically-sized array internally, and is fast enough for most usage. If you're using multi-dimentional arrays, use List<List<List<T>>> if you have to. It's not that much worse in terms of memory, and is much simpler to add items to.

If you're in the 0.1% of usage that requires extreme speed, make sure it's your list accesses that are really the problem before you try to optimize it.

Spectacled answered 16/9, 2008 at 19:29 Comment(0)
P
3

When to abandon the use of arrays

  1. First and foremost, when semantics of arrays dont match with your intent - Need a dynamically growing collection? A set which doesn't allow duplicates? A collection that has to remain immutable? Avoid arrays in all that cases. That's 99% of the cases. Just stating the obvious basic point.

  2. Secondly, when you are not coding for absolute performance criticalness - That's about 95% of the cases. Arrays perform better marginally, especially in iteration. It almost always never matter.

  3. When you're not forced by an argument with params keyword - I just wished params accepted any IEnumerable<T> or even better a language construct itself to denote a sequence (and not a framework type).

  4. When you are not writing legacy code, or dealing with interop

In short, its very rare that you would actually need an array. I will add as to why may one avoid it?

  1. The biggest reason to avoid arrays imo is conceptual. Arrays are closer to implementation and farther from abstraction. Arrays conveys more how it is done than what is done which is against the spirit of high level languages. That's not surprising, considering arrays are closer to the metal, they are straight out of a special type (though internally array is a class). Not to be pedagogical, but arrays really do translate to a semantic meaning very very rarely required. The most useful and frequent semantics are that of a collections with any entries, sets with distinct items, key value maps etc with any combination of addable, readonly, immutable, order-respecting variants. Think about this, you might want an addable collection, or readonly collection with predefined items with no further modification, but how often does your logic look like "I want a dynamically addable collection but only a fixed number of them and they should be modifiable too"? Very rare I would say.

  2. Array was designed during pre-generics era and it mimics genericity with lot of run time hacks and it will show its oddities here and there. Some of the catches I found:

    1. Broken covariance.

       string[] strings = ...
       object[] objects = strings;
       objects[0] = 1; //compiles, but gives a runtime exception.
      
    2. Arrays can give you reference to a struct!. That's unlike anywhere else. A sample:

       struct Value { public int mutable; }
      
       var array = new[] { new Value() };  
       array[0].mutable = 1; //<-- compiles !
       //a List<Value>[0].mutable = 1; doesnt compile since editing a copy makes no sense
       print array[0].mutable // 1, expected or unexpected? confusing surely
      
    3. Run time implemented methods like ICollection<T>.Contains can be different for structs and classes. It's not a big deal, but if you forget to override non generic Equals correctly for reference types expecting generic collection to look for generic Equals, you will get incorrect results.

       public class Class : IEquatable<Class>
       {
           public bool Equals(Class other)
           {
               Console.WriteLine("generic");
               return true;
           }
           public override bool Equals(object obj)
           {
               Console.WriteLine("non generic");
               return true;
           } 
       }
      
       public struct Struct : IEquatable<Struct>
       {
           public bool Equals(Struct other)
           {
               Console.WriteLine("generic");
               return true;
           }
           public override bool Equals(object obj)
           {
               Console.WriteLine("non generic");
               return true;
           } 
       }
      
       class[].Contains(test); //prints "non generic"
       struct[].Contains(test); //prints "generic"
      
    4. The Length property and [] indexer on T[] seem to be regular properties that you can access through reflection (which should involve some magic), but when it comes to expression trees you have to spit out the exact same code the compiler does. There are ArrayLength and ArrayIndex methods to do that separately. One such question here. Another example:

       Expression<Func<string>> e = () => new[] { "a" }[0];
       //e.Body.NodeType == ExpressionType.ArrayIndex
      
       Expression<Func<string>> e = () => new List<string>() { "a" }[0];
       //e.Body.NodeType == ExpressionType.Call;
      
    5. Yet another one. string[].IsReadOnly returns false, but if you are casting, IList<string>.IsReadOnly returns true.

    6. Type checking gone wrong: (object)new ConsoleColor[0] is int[] returns true, whereas new ConsoleColor[0] is int[] returns false. Same is true for uint[] and int[] comparisons. No such problems if you use any other collection types.

How to abandon the use of arrays.

The most commonly used substitute is List<T> which has a cleaner API. But it is a dynamically growing structure which means you can add to a List<T> at the end or insert anywhere to any capacity. There is no substitute for the exact behaviour of an array, but people mostly use arrays as readonly collection where you can't add anything to its end. A substitute is ReadOnlyCollection<T>.

Prototrophic answered 11/11, 2013 at 13:23 Comment(4)
The fact that given Point[] myPts;, a statement myPts[3].X+=3; will affect the X property of myPts[3] without affecting the X property of any other Point anywhere in the universe would seem to me like a good thing. In your example, why shouldn't array[0].mutable be 1? People who want to pretend everything in the universe is a class object may not like structures, but the meaning of an array of a "simple" mutable structure type is much more self-evident than the meaning of an array of a mutable class type.Opportunist
supercat, I really dont have any opinion on whether it's a good thing or bad thing (personally I like reference semantics as that's what I deal with all the time, and never needed to write my own struct). But I consider array's implementation of indexer bad because it isn't consistent with how structs behave elsewhere. array[0].mutable shouldnt be 1 because I'm told and I experience I get a new value every time. That's what all other [] indexers, methods, variable assignment etc do.Prototrophic
The fact that other indexed properties don't behave like array slots is directly analogous to the fact that other non-indexed properties don't behave like fields. Starting with .NET 2.0, it would have been possible to reasonably efficiently have properties behave like variables if for a property of type TProp there were a standard property template AccessValue<TExtra>(ActionByRef<TProp, TExtra> act, ref TExtra extraParam); If such a thing were included in IList<T>, List<T> might have implemented it something like:Opportunist
T IndexedAccessValue<TExtra>(int index, ActionByRef<TProp, TExtra> act, ref TExtra extraParam) extra) { act(ref _arr[index], ref extraParam); } Unfortunately, since the backing array of List<T> is private, there's no way to make a type which is usable as a List<T> but also allows its items to be used as ref parameters in this way.Opportunist
I
2

When the array is resized, a new array must be allocated, and the contents copied. If you are only modifying the contents of the array, it is just a memory assignment.

So, you should not use arrays when you don't know the size of the array, or the size is likely to change. However, if you have a fixed length array, they are an easy way of retrieving elements by index.

Inculpate answered 16/9, 2008 at 19:26 Comment(0)
I
2

ArrayList and List grow the array by more than one when needed (I think it's by doubling the size, but I haven't checked the source). They are generally the best choice when you are building a dynamically sized array.

When your benchmarks indicate that array resize is seriously slowing down your application (remember - premature optimization is the root of all evil), you can evaluate writing a custom array class with tweaked resizing behavior.

Ingratiate answered 16/9, 2008 at 19:29 Comment(0)
T
2

If you're going to be adding/removing elements a lot, just use a List. If it's multidimensional, you can always use a List<List<int>> or something.

On the other hand, lists are less efficient than arrays if what you're mostly doing is traversing the list, because arrays are all in one place in your CPU cache, where objects in a list are scattered all over the place.

If you want to use an array for efficient reading but you're going to be "adding" elements frequently, you have two main options:

1) Generate it as a List (or List of Lists) and then use ToArray() to turn it into an efficient array structure.

2) Allocate the array to be larger than you need, then put the objects into the pre-allocated cells. If you end up needing even more elements than you pre-allocated, you can just reallocate the array when it fills, doubling the size each time. This gives O(log n) resizing performance instead of O(n) like it would be with a reallocate-once-per-add array. Note that this is pretty much how StringBuilder works, giving you a faster way to continually append to a string.

Teucer answered 16/9, 2008 at 19:30 Comment(7)
In .NET, the list is actually an ArrayList, which uses an Array as the backing store. Calling ToArray simply returns a copy of the array that is storing the values. In other words, the List<T> is a wrapper around a resizable array.Inculpate
A list is not all over the place. If you for some reason are boxing value types, then sure, the elements will be spread out, but a List<T> uses an array internally, so in that respect, it won't be worse off.Delectable
I have to vote it down because it's wrong. It may surprise you to learn that all .NET collections are based on arrays. I believe the reason is for locality of reference and reducing GC overhead.Kirkkirkcaldy
If your array is of objects they will still be scattered all over the place. The array will simply be an array of references. If you want the data to be IN the array you need to use structs.Defraud
Also, your O(n) resize implies n items in the array. This is what a resize will always cost because each reference must be copied into the new array. One resize per add would be O(n) add, not O(n) resize.Defraud
@Mike LinkedList isn't based on an Array, but you are right and that is an exception.Afroasiatic
@MikeDimmick no, the reason is much more likely that .Net 1.0 didn't have generics. Eric Lippert mentions this fact in this blog post: blogs.msdn.microsoft.com/ericlippert/2008/09/22/…Anticline
D
2

Generally, if you must have the BEST indexed lookup performance it's best to build a List first and then turn it into a array thus paying a small penalty at first but avoiding any later. If the issue is that you will be continually adding new data and removing old data then you may want to use a ArrayList or List for convenience but keep in mind that they are just special case Arrays. When they "grow" they allocate a completely new array and copy everything into it which is extremely slow.

ArrayList is just an Array which grows when needed. Add is amortized O(1), just be careful to make sure the resize won't happen at a bad time. Insert is O(n) all items to the right must be moved over. Remove is O(n) all items to the right must be moved over.

Also important to keep in mind that List is not a linked list. It's just a typed ArrayList. The List documentation does note that it performs better in most cases but does not say why.

The best thing to do is to pick a data structure which is appropriate to your problem. This depends one a LOT of things and so you may want to browse the System.Collections.Generic Namespace.

In this particular case I would say that if you can come up with a good key value Dictionary would be your best bet. It has insert and remove that approaches O(1). However, even with a Dictionary you have to be careful not to let it resize it's internal array (an O(n) operation). It's best to give them a lot of room by specifying a larger-then-you-expect-to-use initial capacity in the constructor.

-Rick

Defraud answered 16/9, 2008 at 20:16 Comment(0)
S
1

The best thing you can do is to allocate as much memory as you need upfront if possible. This will prevent .NET from having to make additional calls to get memory on the heap. Failing that then it makes sense to allocate in chunks of five or whatever number makes sense for your application.

This is a rule you can apply to anything really.

Sow answered 16/9, 2008 at 19:27 Comment(0)
P
1

A standard array should be defined with a length, which reserves all of the memory that it needs in a contiguous block. Adding an item to the array would put it inside of the block of already reserved memory.

Permutation answered 16/9, 2008 at 19:28 Comment(0)
B
1

Arrays are great for few writes and many reads, particularly those of an iterative nature - for anything else, use one of the many other data structures.

Bourg answered 16/9, 2008 at 19:29 Comment(0)
H
1

You are correct an array is great for look ups. However modifications to the size of the array are costly.

You should use a container that supports incremental size adjustments in the scenario where you're modifying the size of the array. You could use an ArrayList which allows you to set the initial size, and you could continually check the size versus the capacity and then increment the capacity by a large chunk to limit the number of resizes.

Or you could just use a linked list. Then however look ups are slow...

Hunker answered 16/9, 2008 at 19:33 Comment(1)
Don't use ArrayList in .Net 2.0 and later. Use the generic List<T> for the type you are storing.Derouen
S
1

If I think I'm going to be adding items to the collection a lot over its lifetime, than I'll use a List. If I know for sure what the size of the collection will be when its declared, then I'll use an array.

Another time I generally use an array over a List is when I need to return a collection as a property of an object - I don't want callers adding items that collection via List's Add methods, but instead want them to add items to the collection via my object's interface. In that case, I'll take the internal List and call ToArray and return an array.

Sedlik answered 16/9, 2008 at 20:7 Comment(1)
You might want to reconsider returning an array copy of your internal list from a property: #35507Degenerate
Y
1

If you are going to be doing a lot of adding, and you will not be doing random access (such as myArray[i]). You could consider using a linked list (LinkedList<T>), because it will never have to "grow" like the List<T> implementation. Keep in mind, though, that you can only really access items in a LinkedList<T> implementation using the IEnumerable<T> interface.

Yerxa answered 16/9, 2008 at 20:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.