28
votes

Can someone tell me which one is more efficient between List<int> and int[]. Because I am working on a project and as you might know efficiency is way so important concern now.

If you added some introductory note to your post, it'd be great tho :)

6
Beware from the premature optimization. If efficiency is important in your application you'd better write tests that measure the performance of the whole functionality instead of concetranting on micro-optimization. Often the true optimization need a clean design and ofthe micro-optimization hinder a clean design.Andrea Francia
Using the wrong data structure right from the start can seriously impact your performance, its always worth spending the time to choose the right data structure based on your needs, instead of just picking one randomly.Alex Black
If a List is the wrong data structure, it is highly unlikely that an array is the correct one.Dour High Arch
Thats not been my experience. I often use array when I don't need the resizing capability of List.Alex Black
The question is really what kind of time is of essence here? Your development time that your company really pays for, or some minor time savings in the code running? If you spend less time coding by using List<int> because you use automatic resizing, then use it. If you just need a static size array, use int[]. Ref. answer by 280Z28 that you accepted.awe

6 Answers

83
votes
(list should be resizable) ? List<int> : int[]

List<int> is a wrapper for int[] that resizes as needed. With JIT inlining, they should perform almost identically, but the JIT will have an easier time edging out the extra performance from int[] because it's a CLI primitive with dedicated IL instructions.

13
votes

Just for the fun of it, I ran this:

int cap = 100000;

Stopwatch sw1 = new Stopwatch();
sw1.Start();

int[] ix = new int[cap];
for (int x = 0; x < cap; x++)
{
    ix[x] = 1;
}

sw1.Stop();

Stopwatch sw2 = new Stopwatch();
sw2.Start();
List<int> iy = new List<int>(cap);
for (int y = 0; y < cap; y++)
{
    iy.Add(y);
}
sw2.Stop();

Console.WriteLine(cap.ToString() + "     int[]=" + sw1.ElapsedTicks.ToString());
Console.WriteLine(cap.ToString() + " List<int>=" + sw2.ElapsedTicks.ToString());

Console.ReadKey();

And got this:

100000 int[]=1796542
100000 List=2517922

I tried it in elapsed milliseconds and got 0 and 1 respectively. Clearly the int[] is way faster, but unless you're talking huge arrays, I'd say it is just nominal.

10
votes

If you know exactly how many elements are going to be in the collection and don't need any of the extra features of List<int> AND (that is a very serious AND) performance is a serious concern, go with int[]. Otherwise stick with List<int>.

7
votes

the latter is more effective.
In the source code, List<> is named by some Arrays.
Eg, List<Type> aa=new List<Type>();
In general, an array Type[] is declared, the length of it is a certain number. In another word, if you declare a List<>, a big space has already used.
If the List<>'s element is out of length, the array should be copied to another bigger one. So, it is better not to use List<>.
The better way to use is to declared the length of it.
List aa=new List<Type>(10);

6
votes

List uses an array internally, so using an array (correctly) would always be more (or atleast as) efficient.

4
votes

If you plan on using any features that a list would provide (searching, sorting, removing, resizing) then I would go with a list because chances are these functions are very optimized already and you won't be able to write better versions of them.