哪一个更有效:List 或int []

时间:2023-02-10 01:15:18

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.

有人能告诉我哪一个在List 和int []之间更有效。因为我正在研究一个项目,你可能知道效率是如此重要的关注点。

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

如果您在帖子中添加了一些介绍性说明,那就太棒了:)

6 个解决方案

#1


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

List 是int []的包装器,可根据需要调整大小。使用JIT内联,它们应该执行几乎相同的操作,但是JIT将更容易从int []中删除额外的性能,因为它是具有专用IL指令的CLI原语。

#2


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.

我在经过的毫秒中尝试了它,分别得到0和1。显然int []的速度更快,但除非你说的是大型数组,否则我认为它只是名义上的。

#3


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

如果你确切地知道集合中有多少元素,并且不需要List 的任何额外功能AND(这是一个非常严重的AND)性能是一个严重的问题,请使用int []。否则坚持使用List

#4


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

后者更有效。在源代码中,List <>由一些数组命名。例如,List aa = new List ();通常,声明一个数组Type [],它的长度是一定的数字。换句话说,如果声明List <>,则已经使用了大空间。如果List <>的元素超出,则应将数组复制到另一个更大的元素。因此,最好不要使用List <>。更好的使用方法是声明它的长度。列出aa =新列表 (10);

#5


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

List在内部使用数组,因此使用数组(正确)将始终更多(或至少)有效。

#6


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


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

List 是int []的包装器,可根据需要调整大小。使用JIT内联,它们应该执行几乎相同的操作,但是JIT将更容易从int []中删除额外的性能,因为它是具有专用IL指令的CLI原语。

#2


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.

我在经过的毫秒中尝试了它,分别得到0和1。显然int []的速度更快,但除非你说的是大型数组,否则我认为它只是名义上的。

#3


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

如果你确切地知道集合中有多少元素,并且不需要List 的任何额外功能AND(这是一个非常严重的AND)性能是一个严重的问题,请使用int []。否则坚持使用List

#4


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

后者更有效。在源代码中,List <>由一些数组命名。例如,List aa = new List ();通常,声明一个数组Type [],它的长度是一定的数字。换句话说,如果声明List <>,则已经使用了大空间。如果List <>的元素超出,则应将数组复制到另一个更大的元素。因此,最好不要使用List <>。更好的使用方法是声明它的长度。列出aa =新列表 (10);

#5


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

List在内部使用数组,因此使用数组(正确)将始终更多(或至少)有效。

#6


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.

如果你打算使用列表提供的任何功能(搜索,排序,删除,调整大小),那么我会选择一个列表,因为这些功能很可能已经非常优化,你将无法编写更好的版本。