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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top