C# 中最快、最高效的集合类型
我正在构建一个应用程序,需要一个集合来容纳大约 10k 的字符串。
集合将用作队列。
因此,我们正在查看 C# 中的不同集合类型,但无法确定哪一种在队列中执行 Put 和 Get 操作的速度方面具有最佳性能。还应该能够不允许队列/集合中出现重复项。
根据评论进行编辑。
任何现有的收藏都会有所帮助。或者一个可以胜过任何现有集合的自定义集合也会很棒。
谢谢
I am building an application which will require a collection to hold about 10k of Strings.
Collection will be used as queue.
So was looking through different collection types in C# but could not figure out which one has best performance in regards to speed of doing Put and Get operation in Queue. Also should be capable of not allowing duplicates in the Queue/Collection.
EDIT based on the comments..
Any existing collection will be helpful. Or a custom collection which could out perform any existing collection will be great.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您正在寻找高性能看跌期权和在检查唯一性(重复检查)时获取,但顺序并不重要(不是队列),然后使用
HashSet
如果队列功能更重要,则使用
Queue
我认为没有任何东西可以同时提供这两者。
If you are looking for High performance Put & Get while checking for uniqueness (duplicate checking) but order doesnt matter (not a queue) then use
HashSet<T>
If Queue feature is more important then use a
Queue<T>
I dont think there is anything which offer both.
有 OrderedDictionary 类,它保留插入顺序,但允许您通过键查找值。
There is the OrderedDictionary class which keeps the insertion order but allows you to look up values by key.
你介意花费 O(2n) 内存吗?您可以使用 Queue<>与字典<,>结合使用。队列将处理队列和出列操作,字典将确保唯一的条目。一个简单的包装类可以将这两者结合起来,它会给你 O(log n) 的队列和出队时间。
示例:
对此自定义数据结构的插入检查字典是否已包含该项目。此操作使用 ContainsKey 方法,该方法是一个 O(log n) 操作。如果该项目已包含在数据结构中,则该方法退出。如果不包含该项目,则该项目将被插入到队列中,这是一个常数 O(1) 操作。它也将被添加到词典中。当字典的数量小于容量时,这将接近一个常数,插入时间也为 O(1)。因此,总队列时间将为 O(log n)。
出队方法也是如此。
该解决方案与内置数据结构 OrderedDictionary 基本相同,但是,由于该解决方案使用泛型,因此其操作中的装箱/拆箱没有开销,从而使其速度更快。
Do you mind spending O(2n) memory? You could use a Queue<> in combination with a Dictionary<,>. The queue would handle the queue and dequeue operations and the dictionary would ensure unique entries. A simple wrapper class could combine those two, and it would give you O(log n) queue and dequeue times.
Example:
An insert into this custom data structure check if the dictionary already contains the item. This operation uses the ContainsKey method which is a O(log n) operation. If the item was already contained in the data structure than the method exits. If the item isn't contained, then the item will be inserted into the queue which is a constant O(1) operation. It will also be added to the dictionary. When the count of the dictionary is less than the capacity this will approach a constant, O(1) insertion time as well. The total queue time will therefore be O(log n).
The same thing goes the dequeuing method.
This solution is basically the same as the built-in data structure OrderedDictionary, however, since this solution uses generic there is no overhead in boxing/unboxing in it's operations making it wastely faster.