简体   繁体   English

Seq用于快速随机访问和Scala中的快速增长

[英]Seq for fast random access and fast growth in Scala

What would be the best Scala collection (in 2.8+), mutable or immutable, for the following scenario: 对于以下场景,什么是最好的Scala集合(2.8+),可变或不可变的:

  • Sequentially ordered, so I can access items by position (a Seq) 按顺序排序,所以我可以按位置访问项目(一个Seq)
  • Need to insert items frequently, so the collection must be able to grow without too much penalty 需要经常插入物品,因此集合必须能够在没有太多惩罚的情况下成长
  • Random access, frequently need to remove and insert items at arbitrary indexes in the collection 随机访问,经常需要删除和插入集合中任意索引的项目

Currently I seem to be getting good performance with the mutable ArrayBuffer, but is there anything better? 目前我似乎在使用可变的ArrayBuffer获得了良好的性能,但有什么更好的吗? Is there an immutable alternative that would do as well? 是否有一个不可改变的替代品也会这样做? Thanks in advance. 提前致谢。

Mutable: ArrayBuffer Mutable: ArrayBuffer
Immutable: Vector 不变: Vector

If you insert items at random positions more than log(N)/N of the time that you access them, then you should probably use immutable.TreeSet as all operations are O(log(N)). 如果您在随机位置插入项目超过访问它们的时间的log(N)/ N,那么您应该使用immutable.TreeSet因为所有操作都是O(log(N))。 If you mostly do accesses or add to the (far) end, ArrayBuffer and Vector work well. 如果您主要访问或添加到(远)端, ArrayBufferVector工作正常。

Vector . Vector IndSeq from scalaz should be even better. IndSeqscalaz应该会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM