字符串数组和c#中字符串列表的区别是什么?

时间:2022-01-16 13:05:56

I hear on MSDN that an array is faster than a collection.

我在MSDN上听说数组比集合快。

Can you tell me how string[] is faster then List<string>.

你能告诉我string[]比List 快多少吗?

5 个解决方案

#1


50  

Arrays are a lower level abstraction than collections such as lists. The CLR knows about arrays directly, so there's slightly less work involved in iterating, accessing etc.

数组是比列表之类的集合更低层次的抽象。CLR直接了解数组,因此在迭代、访问等方面所涉及的工作稍微少一些。

However, this should almost never dictate which you actually use. The performance difference will be negligible in most real-world applications. I rarely find it appropriate to use arrays rather than the various generic collection classes, and indeed some consider arrays somewhat harmful. One significant downside is that there's no such thing as an immutable array (other than an empty one)... whereas you can expose read-only collections through an API relatively easily.

但是,这几乎不应该指示您实际使用的是什么。在大多数真实应用程序中,性能差异可以忽略不计。我很少发现使用数组而不是各种通用集合类是合适的,而且确实有些人认为数组有些有害。一个重要的缺点是没有不可变数组(除了一个空数组)……然而,您可以相对容易地通过API公开只读集合。

#2


9  

The article is from 2004, that means it's about .net 1.1 and there was no generics. Array vs collection performance actually was a problem back then because collection types caused a lot of exta boxing-unboxing operations. But since .net 2.0, where generics was introduced, difference in performance almost gone.

这篇文章发表于2004年,这意味着它是关于。net 1.1,并且没有泛型。阵列vs收集性能在当时确实是一个问题,因为收集类型导致了很多exta的开箱操作。但是自从。net 2.0引入了泛型之后,性能上的差异几乎消失了。

#3


6  

An array is not resizable. This means that when it is created one block of memory is allocated, large enough to hold as many elements as you specify.

数组不可调整大小。这意味着当它被创建时,一个内存块被分配,足够容纳您指定的所有元素。

A List on the other hand is implicitly resizable. Each time you Add an item, the framework may need to allocate more memory to hold the item you just added. This is an expensive operation, so we end up saying "List is slower than array".

另一方面,列表是可以隐式调整大小的。每次添加一个项时,框架可能需要分配更多内存来保存刚刚添加的项。这是一个昂贵的操作,所以我们最后说“List比array慢”。

Of course this is a very simplified explanation, but hopefully enough to paint the picture.

当然,这是一个非常简单的解释,但希望足够描述这个画面。

#4


3  

An array is the simplest form of collection, so it's faster than other collections. A List (and many other collections) actually uses an array internally to hold its items.

数组是最简单的集合形式,所以它比其他集合要快。列表(和许多其他集合)实际上在内部使用数组来保存其项。

An array is of course also limited by its simplicity. Most notably you can't change the size of an array. If you want a dynamic collection you would use a List.

数组当然也受到其简单性的限制。最明显的是你不能改变数组的大小。如果您想要一个动态集合,您可以使用列表。

#5


3  

List<string> is class with a private member that is a string[]. The MSDN documentation states this fact in several places. The List class is basically a wrapper class around an array that gives the array other functionality.

List 是具有字符串[]的私有成员的类。MSDN文档在几个地方说明了这一事实。List类基本上是一个包装器类,它围绕一个数组来提供数组的其他功能。

The answer of which is faster all depends on what you are trying to do with the list/array. For accessing and assigning values to elements, the array is probably negligibly faster since the List is an abstraction of the array (as Jon Skeet has said).

这个问题的答案取决于你想用列表/数组做什么。对于访问和分配元素的值,数组可能会以更快的速度递减,因为列表是数组的抽象(如Jon Skeet所说)。

If you intend on having a data structure that grows over time (gets more and more elements), performance (ave. speed) wise the List will start to shine. That is because each time you resize an array to add another element it is an O(n) operation. When you add an element to a List (and the list is already at capacity) the list will double itself in size. I won't get into the nitty gritty details, but basically this means that increasing the size of a List is on average a O(log n) operation. Of course this has drawbacks too (you could have almost twice the amount of memory allocated as you really need if you only go a couple items past its last capacity).

如果您希望拥有一个随时间增长的数据结构(获得越来越多的元素),性能(ave. speed)将开始突出列表。这是因为每次调整数组大小以添加另一个元素时,都是O(n)操作。当您向列表添加一个元素(并且列表已经处于容量状态)时,列表的大小将会加倍。我不会细讲细节,但基本上这意味着增加列表的大小是O(log n)操作。当然,这也有缺点(如果您只使用超过其最后容量的几个项目,那么您所分配的内存几乎是您真正需要的两倍)。

Edit: I got a little mixed up in the paragraph above. As Eric has said below, the number of resizes for a List is O(log n), but the actual cost associated with resizing the array is amortized to O(1).

编辑:我把上面这段弄混了。正如Eric在下面所说的,一个列表的调整大小的数量是O(log n),但是与调整数组大小相关的实际成本被摊销为O(1)。

#1


50  

Arrays are a lower level abstraction than collections such as lists. The CLR knows about arrays directly, so there's slightly less work involved in iterating, accessing etc.

数组是比列表之类的集合更低层次的抽象。CLR直接了解数组,因此在迭代、访问等方面所涉及的工作稍微少一些。

However, this should almost never dictate which you actually use. The performance difference will be negligible in most real-world applications. I rarely find it appropriate to use arrays rather than the various generic collection classes, and indeed some consider arrays somewhat harmful. One significant downside is that there's no such thing as an immutable array (other than an empty one)... whereas you can expose read-only collections through an API relatively easily.

但是,这几乎不应该指示您实际使用的是什么。在大多数真实应用程序中,性能差异可以忽略不计。我很少发现使用数组而不是各种通用集合类是合适的,而且确实有些人认为数组有些有害。一个重要的缺点是没有不可变数组(除了一个空数组)……然而,您可以相对容易地通过API公开只读集合。

#2


9  

The article is from 2004, that means it's about .net 1.1 and there was no generics. Array vs collection performance actually was a problem back then because collection types caused a lot of exta boxing-unboxing operations. But since .net 2.0, where generics was introduced, difference in performance almost gone.

这篇文章发表于2004年,这意味着它是关于。net 1.1,并且没有泛型。阵列vs收集性能在当时确实是一个问题,因为收集类型导致了很多exta的开箱操作。但是自从。net 2.0引入了泛型之后,性能上的差异几乎消失了。

#3


6  

An array is not resizable. This means that when it is created one block of memory is allocated, large enough to hold as many elements as you specify.

数组不可调整大小。这意味着当它被创建时,一个内存块被分配,足够容纳您指定的所有元素。

A List on the other hand is implicitly resizable. Each time you Add an item, the framework may need to allocate more memory to hold the item you just added. This is an expensive operation, so we end up saying "List is slower than array".

另一方面,列表是可以隐式调整大小的。每次添加一个项时,框架可能需要分配更多内存来保存刚刚添加的项。这是一个昂贵的操作,所以我们最后说“List比array慢”。

Of course this is a very simplified explanation, but hopefully enough to paint the picture.

当然,这是一个非常简单的解释,但希望足够描述这个画面。

#4


3  

An array is the simplest form of collection, so it's faster than other collections. A List (and many other collections) actually uses an array internally to hold its items.

数组是最简单的集合形式,所以它比其他集合要快。列表(和许多其他集合)实际上在内部使用数组来保存其项。

An array is of course also limited by its simplicity. Most notably you can't change the size of an array. If you want a dynamic collection you would use a List.

数组当然也受到其简单性的限制。最明显的是你不能改变数组的大小。如果您想要一个动态集合,您可以使用列表。

#5


3  

List<string> is class with a private member that is a string[]. The MSDN documentation states this fact in several places. The List class is basically a wrapper class around an array that gives the array other functionality.

List 是具有字符串[]的私有成员的类。MSDN文档在几个地方说明了这一事实。List类基本上是一个包装器类,它围绕一个数组来提供数组的其他功能。

The answer of which is faster all depends on what you are trying to do with the list/array. For accessing and assigning values to elements, the array is probably negligibly faster since the List is an abstraction of the array (as Jon Skeet has said).

这个问题的答案取决于你想用列表/数组做什么。对于访问和分配元素的值,数组可能会以更快的速度递减,因为列表是数组的抽象(如Jon Skeet所说)。

If you intend on having a data structure that grows over time (gets more and more elements), performance (ave. speed) wise the List will start to shine. That is because each time you resize an array to add another element it is an O(n) operation. When you add an element to a List (and the list is already at capacity) the list will double itself in size. I won't get into the nitty gritty details, but basically this means that increasing the size of a List is on average a O(log n) operation. Of course this has drawbacks too (you could have almost twice the amount of memory allocated as you really need if you only go a couple items past its last capacity).

如果您希望拥有一个随时间增长的数据结构(获得越来越多的元素),性能(ave. speed)将开始突出列表。这是因为每次调整数组大小以添加另一个元素时,都是O(n)操作。当您向列表添加一个元素(并且列表已经处于容量状态)时,列表的大小将会加倍。我不会细讲细节,但基本上这意味着增加列表的大小是O(log n)操作。当然,这也有缺点(如果您只使用超过其最后容量的几个项目,那么您所分配的内存几乎是您真正需要的两倍)。

Edit: I got a little mixed up in the paragraph above. As Eric has said below, the number of resizes for a List is O(log n), but the actual cost associated with resizing the array is amortized to O(1).

编辑:我把上面这段弄混了。正如Eric在下面所说的,一个列表的调整大小的数量是O(log n),但是与调整数组大小相关的实际成本被摊销为O(1)。