tags:

views:

435

answers:

7

Hi,

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

Thanks in advance.

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

+1  A: 

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

Shawn Simon
+43  A: 
(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.

280Z28
Couldn't have said it better myself.
Matt Grande
Conditional operator... I love the way you expressed it.
Ramesh
Oh come on now... Who gave me the -1/for what?
280Z28
lately people have been going crazy with the downvotes. +1
Stan R.
@280Z28 don't sweat the downvotes. Everyone is a critic. I thought your answer was fine.
jeffamaphone
+9  A: 

I find myself repeating this all the time:

  1. I suspect that whatever you are doing, this choice is not your biggest performance hit.
  2. You don't say what you mean by "efficient"; something can be efficient in time or efficient in space. You have to pick which you want to optimize for.
  3. It doesn't matter what you pick: you can test either one yourself by creating an instance of both, filling it up with data, doing whatever it is your program might do, and timing it. I'll bet your salary C# has a timer class.
jeffamaphone
Course it has a timer class but you wouldn't use it for that. Should use a profiler
PeteT
Ok,thanks. Sorry for having you repeat your self.
Braveyard
Heh, no need to apologize. I didn't mean to come off sounding as crotchety as that actually sounds upon review. :)
jeffamaphone
@jeffamaphone good to hear that then :)
Braveyard
+5  A: 

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
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>`.
280Z28
+3  A: 

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.

`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.
280Z28
+3  A: 

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
haha awesome.Thanks :)
Braveyard
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
@guardi: He did set a capacity for the list. List<int> iy = new List<int>(cap). In my experience int[] is much faster.
Alex Black
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
@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
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
+2  A: 

the latter is more effective.
In the source code,List<> is named by some Arrays.
Eg, List aa=new List();
In general, a 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 copyed 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(10);

Edwin Tai
Thanks, but I don't know who actually gave thumbs down to your answer.
Braveyard
): in fact it is only from the source code of dotnet.perhaps it is because of my poor english.....
Edwin Tai
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
(: In Chinese I wanna say "xiexie(谢谢)" to you,thanks a lot~
Edwin Tai