This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Re: Alternative to ListBuffer with constant time length?

1 reply
John Kalucki
Joined: 2009-02-13,
User offline. Last seen 42 years 45 weeks ago.

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-
>
>
>

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: Alternative to ListBuffer with constant time length?

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

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland