You want to review a benchmark of Lists and arrays, in a real-world program. You wonder if it is sometimes better to use logic to accommodate an array over the List collection. Here we look at a benchmark in a real program of Lists and arrays, comparing both the lookup performance and the memory usage, using the C# programming language.
=== Memory usage for arrays and Lists === List generic: 6.172 MB Integer array: 5.554 MB === Lookup performance for arrays and Lists === List generic: 1043.4 ms Integer array: 980.2 ms
In this section we look at the code that was tested. Here we ignore practical differences and focus on how the two data structures perform. The two code examples below contrast how you can use an array with more complex logic, and a List with simpler logic.
=== Program that uses List (C#) ===
using System.Collections.Generic;
class Program
{
static void Main()
{
// Compare time to build up a List individually.
List<int> list = new List<int>();
for (int i = 0; i < 60000; i++)
{
list.Add(i);
}
}
}
=== Program that uses array (C#) ===
using System.Collections.Generic;
class Program
{
static void Main()
{
// Compare time to preallocate an array and assign to it.
int[] array = new int[60000];
for (int i = 0; i < 60000; i++)
{
array[i] = i;
}
}
}Description of the code. The List is built up, one by one, at runtime. This means it may have to allocate more times or change the positions in memory during garbage collection.
(See Garbage Collection Visualizations.)
More description. It creates an array. The int array is declared and created all at once. Thus it will store all values in neighboring memory and won't need to go through any reallocations.
Here we see an example of how arrays and Lists can change performance and memory usage. The benchmarks here were taken from a more complex program, but they show the general pattern of arrays being more efficient.
Findings from benchmark. What I found was that the arrays were about 7% faster, even when other code is involved. The usage was with a data structure that looks up items in an array or List. This article is based on .NET 3.5 SP1.
Memory usage. Regarding memory usage, the int array was considerably more compact than the List generic. There can be substantial overhead when using generics instead of arrays. Please see the memory figures at the top of this article.
No. If you need the advanced features of generics, use them. However, you might be able to convert from generics to arrays. You could store the size of the array and use it when initializing later. That way you don't have to resize anything.
More details. The findings in this article are only useful to you as a hint of how you can influence the performance of your application by changing from a List to an array. The article can't establish whether arrays or Lists are faster. It can tell us that sometimes you can optimize by changing a List to an array.
Here we saw a comparison of List and array memory usage in the C# programming language. For speed, it's sometimes worthwhile to prefer regular arrays. The performance benefit is significant enough that changing your code may be worth your time, assuming the new version doesn't add too much complexity.