28

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 :)

Handcraftsman
  • 6,863
  • 2
  • 40
  • 33
Tarik
  • 79,711
  • 83
  • 236
  • 349
  • 10
    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 Jul 23 '09 at 00:15
  • 6
    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. – waterlooalex Jul 23 '09 at 00:42
  • 6
    If a List is the wrong data structure, it is highly unlikely that an array is the correct one. – Dour High Arch Jul 23 '09 at 00:56
  • Thats not been my experience. I often use array when I don't need the resizing capability of List. – waterlooalex Jul 23 '09 at 01:01
  • 2
    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` 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 Apr 06 '10 at 05:48
  • possible duplicate of [Performance of Arrays vs. Lists](http://stackoverflow.com/questions/454916/performance-of-arrays-vs-lists) – nawfal Nov 11 '13 at 07:09

6 Answers6

84
(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.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
13

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.

Cyberherbalist
  • 12,061
  • 17
  • 83
  • 121
  • 1
    If you set the initial capacity in the List constructor just as you did for the array, I would suspect that the performance would be much closer. – Eddie Velasquez Jul 23 '09 at 00:28
  • 4
    @guardi: He did set a capacity for the list. List iy = new List(cap). In my experience int[] is much faster. – waterlooalex Jul 23 '09 at 00:41
  • 1
    A single run of a test like this is meaningless. You need to run the test many times and average the results. But List will probably always be slower due to the method call overhead a class implies. – orj Jul 23 '09 at 01:43
  • 5
    @orj, how could it be "meaningless". It might be more meaningful to do what you said, but it is hardly meaningless. Harsh of you, and not at all accurrate. Besides, how many times do you think I ran the thing before posting this response? It wasn't just once, and the result posted was typical of all the runs. I didn't bother averaging them, but there didn't seem to be a need to do a "full up" benchmark. – Cyberherbalist Jul 23 '09 at 15:45
  • 1
    Try this again with a reference type instead of a value type. I suspect your biggest performance hit here is coming from boxing and unboxing the int for the list. – overstood Oct 12 '09 at 01:18
  • 5
    @overstood: There is no boxing/unboxing of the integer. If the generic parameter `T` is a value type, then `T[]` will be a array of values, not an array of boxed values. – Lou Sep 10 '12 at 13:15
10

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>.

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • 3
    Most of `List`'s features are available as static members of the `Array` class when working with `T[]`. If the list is fixed size, use `T[]`, as both implement `IList`. – Sam Harwell Jul 23 '09 at 00:15
7

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);

Vikrant
  • 4,920
  • 17
  • 48
  • 72
Edwin Tai
  • 1,224
  • 1
  • 13
  • 19
  • 3
    Your English might be poor, but that's no reason to bump your answer down. Uncharitable jerks do exist on this site, and if they couldn't follow what you wrote, then they should have just let it stay unrated. I bumped you up just for good will. Peace. – Cyberherbalist Jul 23 '09 at 15:54
  • 1
    I don't think people are bumping down because of poor English. As several other answers have pointed out, the performance difference is only significant if you have hundreds of thousands of records. For many purposes, a List<> gives you way more functionality at a reasonable cost. Only if you have 1e6 or more entries AND you know exactly how many entries AND performance is REALLY a concern should you optimize to an array. The answer is being voted down (not by me, mind you) because it's a categorical recommendation when many (if not most, IMO) people would be ill-served by its advice. – Michael Blackburn Mar 29 '16 at 20:58
6

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

Shawn
  • 19,465
  • 20
  • 98
  • 152
  • Thanks for a short answer ... clear, concise. Nothing else needs to be said. – steve Feb 19 '16 at 17:58
  • 1
    @steve Except for the question's specific request for explanatory commentary, sure. – Michael Blackburn Mar 29 '16 at 21:00
  • Dear Mr. Blackburn: Please keep your snide comments to yourself as they have no place here. Further, the question certainly is not specific about asking for explanation. Maybe you think the part about 'introductory note' is asking for explanation, but I think it's gibberish and therefore to be ignored. Lastly, I think this is a very good answer to the question (even though it has few votes) but sadly your comment diminishes this answer. – steve Mar 31 '16 at 14:32
4

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.

user106596
  • 116
  • 2
  • 2
    `List.Sort` just calls `Array.Sort` on the internal data array, which is available for use on any array. Same with searching. The only real thing `List` offers is automatic resizing and `ForEach()`, but I imagine the enumerator for `T[]` is even faster because it doesn't have to check whether the array was changed. – Sam Harwell Jul 23 '09 at 01:08