Skip to content
Snippets Groups Projects
01-intersectionWrong.scala 839 B
Newer Older
//    PDF: https://moodle.epfl.ch/pluginfile.php/3175938/mod_folder/content/0/week03-4-Scala-Parallel-Collections.pdf?forcedownload=1
//  Video: https://mediaspace.epfl.ch/playlist/dedicated/31866/0_2dyv5wql/0_y9b53u6a
// Slides: 27

package lecture3

import scala.collection.parallel.CollectionConverters.IterableIsParallelizable
import scala.collection.parallel.immutable.ParSet

def parIntersectionWrong(a: ParSet[Int], b: ParSet[Int]) =
  val result = collection.mutable.Set[Int]()
  for x <- a do
    if b.contains(x) then
      result += x
  result

@main def intersectionWrong =
  val a = (0 until 1000).toSet
  val b = (0 until 1000 by 4).toSet
  val seqRes = a.intersect(b)
  val parRes = parIntersectionWrong(a.par.toSet, b.par.toSet)
  println(s"Sequential result: ${seqRes.size}")
  println(s"Parallel result: ${parRes.size}")