简体   繁体   中英

Using scala parallelism when iterating over a java converted List to immutable

I'm attempting to speed up the execution time using scala parallelism.

So to convert a java ArrayList to an immutable one I use :

var imList = scala.collection.JavaConversions.asScalaBuffer(normalQLFolderList)

and then to take advantage of multiple cores when iterating I use :

for (i <- imList .par) {
}

Am I taking advantage of scala parallelism in the correct way ? In this case iterating over a list. Is there a large performance hit on asScalaBuffer ?

Collections which can be converted into their parallel counterparts in constant time include mutable and immutable hash maps and hash sets, ranges, vectors and arrays. For all other collection types, including wrappers around collections coming from Java, calling par results in copying the contents of the collection into a format more suitable for parallelization.

This is described here in more detail:

http://docs.scala-lang.org/overviews/parallel-collections/conversions.html

However, depending on how big the collection is, and how expensive the for block is, it might be perfectly reasonable to pay for this conversion. The more processing the parallel for block does per each element, the more the cost of the conversion is amortized.

I would say that if the computation per each element involves anything nontrivial (eg it at least creates new objects) paying for the conversion makes sense, but a good idea is to measure the performance difference between the sequential version and the parallel version which includes calling par :

http://docs.scala-lang.org/overviews/parallel-collections/performance.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM