Scala parallel programming, parallel collections, .par, and performance

Summary: This short post shows a few examples of using parallel collections in Scala.

To be clear, these examples of using Scala parallel collections aren’t my own examples, they come from this page on the website. But, for the completeness of my Scala cookbook recipes, I wanted to make sure I included a reference to parallel collections here. (I do have other examples of using parallel collections in Scala on this site.)

First, here’s an example of converting a normal List to a parallel list (technically a ParSeq) so you can then run a parallel map method to transform a collection of String objects to all-uppercase strings:

scala> val lastNames = List("Smith","Jones","Frankenstein","Bach","Jackson","Rodin").par
lastNames: scala.collection.parallel.immutable.ParSeq[String] 
       = ParVector(Smith, Jones, Frankenstein, Bach, Jackson, Rodin)

res0: scala.collection.parallel.immutable.ParSeq[String] 

And here’s a second example of how to convert a map operation on a List from the usual approach:

val list = (1 to 10000).toList + 42)

to using the map method on a parallel collection by again invoking .par, this time without creating the explicit intermediate reference (as was done with lastNames in the previous example): + 42)

Again, these Scala parallel programming examples come from this page on the official Scala website. I’ll add my own examples here in time, but until I get a chance to write those, I wanted to make sure that people who are using my Scala cookbook as a reference had a link to a great parallel programming resource.

Also, here’s a link to a great Measuring Performance article on the official Scala website.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.