Skip to content
Snippets Groups Projects
Unverified Commit 6586fff1 authored by Hamza Remmal's avatar Hamza Remmal :homes:
Browse files

Add Intersection Correct example from lecture 3

parent 526b675b
No related branches found
No related tags found
No related merge requests found
......@@ -5,14 +5,14 @@ import scala.collection.parallel.CollectionConverters.IterableIsParallelizable
import org.scalameter._
// TODO : THIS DOES NOT COMPILE
def intersection(a: GenSet[Int], b: GenSet[Int]): Set[Int] =
def intersection_wrong(a: GenSet[Int], b: GenSet[Int]): Set[Int] =
val result = mutable.Set[Int]()
for (x <- a) if (b contains x) result += x
result
@main def intersectionWrong =
val seqres = intersection((0 until 1000).toSet, (0 until 1000 by 4).toSet)
val parres = intersection((0 until 1000).par.toSet, (0 until 1000 by 4).par.toSet)
val seqres = intersection_wrong((0 until 1000).toSet, (0 until 1000 by 4).toSet)
val parres = intersection_wrong((0 until 1000).par.toSet, (0 until 1000 by 4).par.toSet)
log(s"Sequential result - ${seqres.size}")
log(s"Parallel result - ${parres.size}")
......
package lecture3
import org.scalameter.*
import java.util.*
import java.util.concurrent.ConcurrentSkipListSet
import scala.collection.*
import scala.collection.parallel.CollectionConverters.IterableIsParallelizable
// TODO : THIS DOES NOT COMPILE
def intersection_correct(a: GenSet[Int], b: GenSet[Int]) =
val result = new ConcurrentSkipListSet[Int]()
for (x <- a) if (b contains x) result add x
result
@main def intersectionCorrect =
val seqres = intersection_correct((0 until 1000).toSet, (0 until 1000 by 4).toSet)
val parres = intersection_correct((0 until 1000).par.toSet, (0 until 1000 by 4).par.toSet)
log(s"Sequential result - ${seqres.size}")
log(s"Parallel result - ${parres.size}")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment