- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Re: Alternative to ListBuffer with constant time length?
Fri, 2009-02-13, 18:57
I just profiled a complicated server application and also discovered, to my
surprise, that ListBuffer.length() is indeed O(n). As a Java programmer, I
blindly expected a constant-time length() operation for ListBuffer, although
I can't say for sure if a Java Queue or Dequeue has constant time length()
either...
I wonder what the collection class design philosophy is that would not make
this linear? Considering moving down to a LinkedBlockingQueue, as I need
synchronization as well.
-John Kalucki
Sciss-3 wrote:
>
> hi,
>
> i need the functionality of scala.collection.mutable.ListBuffer
> (constant time 'prepend' and 'append') along with a constant time
> 'length'. the docs state ListBuffer calculates length in linear
> time... What choice do i have?
>
>
> thanks, -sciss-
>
>
>
On Fri, Feb 13, 2009 at 6:57 PM, John Kalucki wrote:
>
> I just profiled a complicated server application and also discovered, to my
> surprise, that ListBuffer.length() is indeed O(n). As a Java programmer, I
> blindly expected a constant-time length() operation for ListBuffer, although
> I can't say for sure if a Java Queue or Dequeue has constant time length()
> either...
>
> I wonder what the collection class design philosophy is that would not make
> this linear? Considering moving down to a LinkedBlockingQueue, as I need
> synchronization as well.
>
It was an oversight. 2.8 collections will have a constant time length
for ListBuffers. Meanwhile, there's always ArrayBuffer.
Cheers