I am trying to work more with an immutable scalas collection, as it is easy to parallelize, but I am struggling with some newbie problems. I am looking for a way to create a (efficiently) new vector from an operation. To be precise, I want something like
val v : Vector[Double] = RandomVector(10000)
val w : Vector[Double] = RandomVector(10000)
val r = v + w
I tested the following:
// 1)
val r : Vector[Double] = (v.zip(w)).map{ t:(Double,Double) => t._1 + t._2 }
// 2)
val vb = new VectorBuilder[Double]()
var i=0
while(i<v.length){
vb += v(i) + w(i)
i = i + 1
}
val r = vb.result
}
Both are very long compared to working with an array:
[Vector Zip/Map ] Elapsed time 0.409 msecs
[Vector While Loop] Elapsed time 0.374 msecs
[Array While Loop ] Elapsed time 0.056 msecs
Is there a better way to do this? I think that working with zip / map / reduce has the advantage that it can work in parallel as soon as collections support this.
thank
source
share