开发者

What is the running time for size on Scala's ListBuffer?

开发者 https://www.devze.com 2023-03-28 16:01 出处:网络
Is it constant or O(n)? If O(n) are there similar data structures with constant time siz开发者_如何学运维e operations?Strangely, size and length have different descriptions in the ListBuffer docs. For

Is it constant or O(n)? If O(n) are there similar data structures with constant time siz开发者_如何学运维e operations?


Strangely, size and length have different descriptions in the ListBuffer docs. For sure, ListBuffer.length is constant time. Prior to Scala 2.8, length was indeed O(n), but this is now fixed. The implementation of size in TraversableOnce suggests that it is O(n), but I may be missing something.

Other performance characteristics of Scala collections are documented here. For ListBuffer specifically,

             head     tail     apply   update   prepend  append  insert
 ListBuffer  C        L        L       L        C        C       L

where C is constant and L is linear time.

Edit : Both ListBuffer length and size are now O(1) - The issue mentioned by @KiptonBarros was closed with Scala 2.9.1 see: https://issues.scala-lang.org/browse/SI-4933

0

精彩评论

暂无评论...
验证码 换一张
取 消