频繁插入到分类收集频繁

2023-09-03 23:03:35 作者:笑.破红尘

我已经整理集合(列表),我需要把它排在所有时间。

I have sorted collection (List) and I need to keep it sorted at all times.

我目前使用我的收藏List.BinarySearch,然后插入正确的地方元素。我也想尽插入,但无法接受的表现后分类列表。

I am currently using List.BinarySearch on my collection and then insert element in right place. I have also tried sorting list after every insertion but the performance in unacceptable.

有没有一个解决方案,将提供更好的性能?也许我应该用其他的集合。

Is there a solution that will give better performance? Maybe I should use other collection.

(我知道排序列表,但它仅限于独特的键)

(I am aware of SortedList but it is restricted to unique keys)

推荐答案

PowerCollections 有一个 OrderedBag 键入这可能是好你所需要的。从文档

PowerCollections has an OrderedBag type which may be good for what you need. From the docs

插入,删除,并查找一个   一个元素都在日志(N)+ M做   时间,其中N是按键的数量   树,M是当前数   的元素的副本被   办理。

Inserting, deleting, and looking up an an element all are done in log(N) + M time, where N is the number of keys in the tree, and M is the current number of copies of the element being handled.

不过,对于.NET 3.5内置的类型,使用 List.BinarySearch 并插入每个项目到正确的地方是一个良好的开端 - 但在内部使用数组所以你的性能会下降,由于所有的复制你做的,当你插入。

However, for the .NET 3.5 built in types, using List.BinarySearch and inserting each item into the correct place is a good start - but that uses an Array internally so you're performance will drop due to all the copying you're doing when you insert.

如果您可以将您的镶入批次,将改善的事情,但除非你能毕竟你插入你最好还是使用 OrderedBag 从 PowerCollections 如果可以的话。

If you can group your inserts into batches that will improve things, but unless you can get down to only a single sort operation after all your inserting you're probably better off using OrderedBag from PowerCollections if you can.

 
精彩推荐
图片推荐