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?
-
2If you use the array a lot then this will pay off. It is a small optimization, though.usr– usr2015-04-01 08:45:32 +00:00Commented Apr 1, 2015 at 8:45
-
1It depends on your use case. Micro-optimization is rarely important. What are you doing with the array/list?L-Four– L-Four2015-04-01 08:47:20 +00:00Commented 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?Ashkan Mobayen Khiabani– Ashkan Mobayen Khiabani2015-04-01 08:47:41 +00:00Commented Apr 1, 2015 at 8:47
-
2" but they are much slower" i strongly doubt that, lists also use arrays to store the items.Tim Schmelter– Tim Schmelter2015-04-01 08:54:00 +00:00Commented 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.Tim Schmelter– Tim Schmelter2015-04-01 08:56:13 +00:00Commented Apr 1, 2015 at 8:56
5 Answers
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.
Comments
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
ToArray()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
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
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.