13.12. Using Parallel Collections
Problem
You want to improve the performance of algorithms by using parallel collections.
Solution
When creating a collection, use one of the Scala’s parallel collection classes, or convert an existing collection to a parallel collection. In either case, test your algorithm to make sure you see the benefit you’re expecting.
You can convert an existing collection to a parallel collection.
To demonstrate this, first create a sequential collection, such as a
Vector
:
scala> val v = Vector.range(0, 10)
v: scala.collection.immutable.Vector[Int] = Vector(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
Next, print the sequence, and you’ll see that it prints as usual:
scala> v.foreach(print)
0123456789
As expected, that example prints the string 0123456789
. No matter how many times you print
it, you’ll always see that same result; that’s the linear world you’re
used to.
Next, call the par
method on
your collection to turn it into a parallel collection, and repeat the
experiment:
scala>v.par.foreach(print)
5678901234 scala>v.par.foreach(print)
0123456789 scala>v.par.foreach{ e => print(e); Thread.sleep(50) }
0516273894
Whoa. Sometimes the collection prints in order, other times it prints in a seemingly random order. That’s because it’s now using an algorithm that runs concurrently. Welcome to the brave, new, parallel world.
That example showed how to convert a “normal” collection to a parallel collection. You can also create a parallel collection directly:
scala> import scala.collection.parallel.immutable.ParVector ...
Get Scala Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.