2

Arrays are very fast but you can not add anything to array on the other hand Lists are very handy you can add new items without worrying but they are much slower I first use Lists to add my Items and then convert it to Array by MyList.ToArray() and then I process the data from array. Now I'm wondering is this really effective speed wise? or I'm just wasting even more time by converting to array?

11
  • 2
    If you use the array a lot then this will pay off. It is a small optimization, though. Commented Apr 1, 2015 at 8:45
  • 1
    It depends on your use case. Micro-optimization is rarely important. What are you doing with the array/list? Commented Apr 1, 2015 at 8:47
  • imagine that I wont use array a lot then what? list uses the time I was trying to avoid when adding? Commented Apr 1, 2015 at 8:47
  • 2
    " but they are much slower" i strongly doubt that, lists also use arrays to store the items. Commented Apr 1, 2015 at 8:54
  • 3
    @AshkanMobayenKhiabani: then you have measured the wrong things or the way you've measured was wrong. You should show that. Also, 10 times slower could also mean 1ms/10ms in 1000000 iterations which would be nothing under real circumstances. Commented Apr 1, 2015 at 8:56

5 Answers 5

5

MSDN says ToArray is O(N) where N is count: https://msdn.microsoft.com/en-us/library/x303t819%28v=vs.110%29.aspx

The elements are copied using Array.Copy, which is an O(n) operation, where n is Count.

This method is an O(n) operation, where n is Count.

Therefore you're basically going to process the list twice: once for the ToArray call and again in your next loop to actually process the list. Removing the ToArray will probably be more performant than doing the conversion in most cases.

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

Comments

3

Accessing array elements by index is faster than accessing list elements by index. The reason for that can be found in the source code of the .NET Framework. Each access to a list element incurs an explicit range check. The current JIT is not capable of optimizing it out even under the most obvious circumstances (a loop iterating from 0 to list.Count).

Converting a list to an array has a one-time O(N) cost. This cost is not trivial. You have to access the array a lot in order to amortize the cost.

I can't tell you where the tipping point is. This must be measured. As a subjective guess I'd say that iterating 10-100 times over the collection would make this worthwhile.

TL;DR: If you use the array a lot then this will pay off. Usually, this technique is either detrimental or a waste of developer time.

2 Comments

A better option would be to avoid the List entirely - you can use LINQ or iterators to create the items and store them directly to an array using ToArray()
Not using lists explicitly is a good thing. ToArray uses a list-like buffer under the hood, though. You don't around buffering that way.
1

Because resizing an array is time expensive, the only discriminant question I ask myself when I have to choose list vs array is: does the size of the collection is fixed ?

  • Yes: Use an array
  • No: Use a list

Now say you use a list. Converting it to an array before using it is not necessary as Jun says.

Unless you run through the data a lot of time (and the conversion is sufficiently amortize as usr says), the conversion should be done only when necessary (third-party API call who need an array).

4 Comments

Well when the size is known there is no doubt what to use. what I'm wondering is that is that worth converting list to array when the size is unknown or its just better to use the list itself
I have read your question to fast. @Jun response is the good one for me.
A list stored its data in an array so there is no real performance difference. If you want to add/remove items, you use a List, if you want a read-only structure, you use an arrayt
Does not answer the question because this does not touch on performance at all.
0

If your array/list is used rarely, micro-optimization is not important and you should not spend your time in optimizing something that is not going to make any difference. Instead, make the code maintainable, easy to read and less error-prone, which you achieve by using (generic) lists. Using lists also allows you to use LINQ to query them, again a win.

4 Comments

This is not a good answer because it does not answer the question at all. it is generic advice that could be pasted into each and every performance question. It does not help. Also, it falsely assumes that this micro-optimization is not worth it. Where does this assumption come from?!
@usr if you check all answers, you'll see they all have one downvote
Thank god I posted mine after the serial downvoter already left.
@usr: Assumption given by TS: 'imagine that I wont use array a lot then what?'. Also, this answer is advice that explains that he is looking for performance gains when it's not needed. Thanks anyway for your feedback.
-3

The using area a bit different.

Better to use Arrays in not big data.If size is bigger like 100 or more better to use Lists.

Yes there is speed difference depending the size .List is better in big data.

3 Comments

Do you have any sources or examples ?
A list is definitely not better/faster in whatever you could call "big data". Internally, a list contains an array and any time this array is filled, it creates a new, larger one and copies the contents of the old array. That's why it's important to specify a List's capacity in advance, to avoid constant resize operations
'Big data' is capacity of array or list.Yes you are absolutely right.In array it is also the same if you want to re-size it,it creates new larger one and copies old one to it. And that is why arrays must be fixed size.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.