0

I want to have the best performance and I know that array its faster than list but with array I need to create a variable for counter and even may need to use .Count or .Length to find the size so I though maybe better just use list? Below are the examples.

Example 1:

foreach (var item in items)
    ItemCollection.Add(item);

Example 2:

int i = 0;

foreach (var item in items)
{
    ItemCollection[i] = item;
    i++;
}

Example 3:

for (int i = 0; i < items.Count; i++)
    ItemCollection[i] = item;
biox
  • 1,526
  • 5
  • 17
  • 28
  • who said an array is faster than a list? – Sayse Aug 01 '13 at 20:41
  • @Sayse http://stackoverflow.com/questions/454916/performance-of-arrays-vs-lists – biox Aug 01 '13 at 20:42
  • 2
    Even your own link says use list - ". But unless you need to micro-optimise, keep it simple and use List etc." Either way its off topic so I apologise for that – Sayse Aug 01 '13 at 20:43
  • 3
    The question shouldn't first be "which is faster", the question should first be which is the right container for your use-case. Most of the time, `List` is preferred as it's more flexible. – James Michael Hare Aug 01 '13 at 20:44
  • 2
    The best is to initialize your ItemCollection with required capacity first. Other micro-optimizations don't really matter. – Alex Skalozub Aug 01 '13 at 20:44
  • Also, if you know the number of items you want to put in a `List` in advance, you can pre-size it. For example if you know you need a list that will hold 13 items `var l = new List(13);` - this gives it an initial capacity instead of going through the "grow & copy" stages up to that point. – James Michael Hare Aug 01 '13 at 20:46
  • I would also try `var ItemCollection=items.ToArray()` and see if such micro optimizations are needed. – I4V Aug 01 '13 at 21:01
  • 2
    You've written the code three ways. If you want to know which is faster, **run them and then you'll know**. – Eric Lippert Aug 01 '13 at 21:24

3 Answers3

4

Example one is your best option as it appears you are trying to dynamically change the size of your array/list,

Example two is just silly.

And example 3 would become tricky when you wish to extend the array. See my first point

A point to note in your third example is in your for loop you have

 for (int i = 0; i < items.Count; i++)

This will revaluate items.Count every iteration so you could micro-optimize by moving this out of the for loop

var length = items.Count
for (int i = 0; i < length; i++)
Sayse
  • 42,633
  • 14
  • 77
  • 146
3

Performance of a list is nearly identical to that of an array. If you know the exact number of items that you are planning to add, you can eliminate the potential memory overhead as well by creating a list with the exact number of elements to avoid re-allocations on Add:

// Reserve the required number of spots in the list
var ItemCollection = new List<ItemType>(items.Count);
foreach (var item in items)
    // Add is not going to cause reallocation,
    // because we reserved enough space ahead of time
    ItemCollection.Add(item);

In most instances, this turns out to be a premature micro-optimization.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • So if I know the length I should add it? Even if I need to do extra calculation (items.Count)? – biox Aug 01 '13 at 20:51
  • @xoemab As far as I know, `items.Count` does not require a calculation in any of the standard collections. The count is maintained on each write, so `items.Count` remains fast even when you call it on a linked list. – Sergey Kalinichenko Aug 01 '13 at 20:53
  • @dasblinkenlight IIRC concurrent collections Computes the `count` on demand – Sriram Sakthivel Aug 01 '13 at 21:02
  • @SriramSakthivel You're right, I wasn't thinking about concurrent collections, only from the `System.Collections.Generic` namespace. Thanks for the correction! – Sergey Kalinichenko Aug 01 '13 at 21:05
0

Well you can use 'foreach' on Arrays :

int[] bob = new int[] { 0, 1, 2, 3 };

foreach (int i in bob)
{
    Console.WriteLine(i);
}

Anyway, in most cases, the difference should be pretty negligible. You also have to realize that 'foreach' doesn't magically iterate through the list, it calls 'GetEnumerator' and then uses this to loop, which also uses some ram (actually more than just creating 'int i').

I generally use Arrays when I know the length is fixed and will remain pretty small, otherwise using Lists is just a lot easier.

Also, don't optimize until you know you need to, you're pretty much wasting your time otherwise.