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

6
  • 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. Commented Jul 23, 2009 at 0: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. Commented Jul 23, 2009 at 0:42
  • 6
    If a List is the wrong data structure, it is highly unlikely that an array is the correct one. Commented Jul 23, 2009 at 0:56
  • Thats not been my experience. I often use array when I don't need the resizing capability of List. Commented Jul 23, 2009 at 1: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<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. Commented Apr 6, 2010 at 5:48

6 Answers 6

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

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

Comments

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.

6 Comments

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.
@guardi: He did set a capacity for the list. List<int> iy = new List<int>(cap). In my experience int[] is much faster.
A single run of a test like this is meaningless. You need to run the test many times and average the results. But List<int> will probably always be slower due to the method call overhead a class implies.
@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.
@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.
|
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>.

1 Comment

Most of List<T>'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<T>.
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);

2 Comments

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

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

3 Comments

Thanks for a short answer ... clear, concise. Nothing else needs to be said.
@steve Except for the question's specific request for explanatory commentary, sure.
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.
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.

1 Comment

List<T>.Sort just calls Array.Sort<T> on the internal data array, which is available for use on any array. Same with searching. The only real thing List<T> 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.