If your calculations should do anything remotely computationally intensive, use Array , either raw or wrapped in your own classes. You can provide a collection-compatible wrapper, but make it an explicit wrapper only for interoperability. Everything except Array is generic and therefore boxed and therefore relatively slow and cumbersome.
If you are not using Array , people will be forced to give up any things that you have and just use Array instead of having a performance impact. Maybe everything is in order; perhaps you want the calculations to be there for convenience, not efficiency. In this case, I suggest using the IndexedSeq interface for the interface, assuming you want people to know that indexing is not overly slow (for example, it is not a List ) and uses Vector under the hood. You will use about 4 times more memory than Array[Double] , and 3-10x slower for most low-force operations (such as multiplication).
For example, this:
val u = v.map(1.0 / _)
about three times slower than this:
val u = new Array[Double](v.length) var j = 0 while (j<u.length) { u(j) = 1.0/v(j) // v is Array[Double] j += 1 }
If you use the map method on Array , it is as slow as the Vector[Double] path; Array operations are common and therefore boxed. (And that's where most fines start.)
source share