性能问题:StringCollection 与 List
我想知道什么时候应该使用 List< string > 以及何时应该使用 StringCollection。
假设我必须处理大量字符串(例如 10mb 的文本文件)。
我知道列表 T> 提供比 字符串集合。
但有时我会发现列表< T>例如,当告诉 Gridview 它的数据源是一个 List慢 时字符串>...
那么有人知道这些集合在内存速度和重量方面的优缺点吗?
关于它们的功能,我相信每个人都会同意说 List 是最好的,所以我的问题不是关于那。考虑问题是关于 Frameworks 4.0 上的项目,因此两者都可以使用。
I was wondering when I should use List< string > and when I should use StringCollection.
Let's say that I have to deal with large number of strings (like text files of 10mb).
I know that List< T > provides more powerful functions than
StringCollection.
But sometimes I kind of find the List< T > slow when for example telling a Gridview that its datasource is a List< String >...
So do anyone know the pros and cons of these collections, concerning speed and weight in memory?
Concerning their functionalities, I am sure everybody will agree to say that List is the best, so my question is not about that. Consider the question is about projects on Frameworks 4.0, so both can be used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我个人更喜欢使用
List
:IEnumerable
而不仅仅是IEnumerable
,因此支持 LINQ我真的很惊讶地发现
StringCollection
比List
- 看看是否可以用数字来支持它。我犹豫的唯一原因是 GridView 可能对 StringCollection 进行硬编码支持,以使其能够快速使用该类型 - 但这对我来说听起来不太可能。I would personally prefer to use
List<string>
:IEnumerable<T>
rather than justIEnumerable
, and thus supports LINQI would be really surprised to find
StringCollection
to be significantly faster thanList<string>
- see if you can back that up with numbers. My only cause for hesitation is thatGridView
could potentially have hard-coded support forStringCollection
to make it fast with that type - but that sounds pretty unlikely to me.在性能和效率方面,它们将非常相似。
List
实际上可能会快一点。它是前通用 ArrayList 的包装器。没有装箱/拆箱,但在幕后仍然有一两个额外的步骤,IIRC。StringCollection 在 .NET 2.0 之前很方便,因为它被强类型化为字符串,这是需要列表的常见事物。不过,我建议现在使用
List
。由于大多数框架和第 3 方程序集将使用它而不是 StringCollection,这将:In terms of performance and efficiency, they will be very similar.
List<string>
might be a little faster actually. It is kindof a wrapper around the pre-generic ArrayList. There's no boxing/unboxing, but there is still an extra step or two under the hood, IIRC.StringCollection was handy before .NET 2.0 because it was strongly typed to string, very common thing to want a list of. I would suggest using
List<string>
now though. Since most framework and 3rd party assemblies will use it rather than StringCollection, this would:List
不是ArrayList
的包装器,它是通过数组实现的
ArrayList
的新实现(将其大小调整为双倍)当count
大于其长度时的大小)和count
属性。List<string>
is not a wrapper overArrayList
It is a new implementation of
ArrayList
implemented by having an array (which is resized to the double size when thecount
becomes bigger than its length) and acount
property.