Question

I have this simple query to run and I want to parallelize it:

val res : Array[Int] = valuesRand
.groupBy(v => v)
.toSeq
.sortBy(_._1)
.map(_._2.size)
.toArray

Is the following the performant equivalent of my query?

val res : Array[Int] = valuesRand
.par
.groupBy(v => v)
.par
.toIndexedSeq
.sortBy(_._1)
.map(_._2.size)
.toArray

I am asking that, because I couldn't find the sortBy method into ParSeq, although it exists in Seq.

Was it helpful?

Solution

The ParSeq class does not directly extend SeqLike which provides for the sortBy. You can see this from the definition of ParSeqLike:

trait ParSeqLike[+T, +Repr <: ParSeq[T], +Sequential <: scala.Seq[T] with SeqLike[T, Sequential]] extends GenSeqLike[T, Repr] with ParIterableLike[T, Repr, Sequential]

which ParSeq extends via

ParSeqLike[T, ParSeq[T], scala.Seq[T]]

Hence, only the Splitter in the underlying implementation is able to do a sortBy which does not help you one bit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top