Skip to content

Instantly share code, notes, and snippets.

@amir343
Created October 17, 2011 09:17
Show Gist options
  • Save amir343/1292276 to your computer and use it in GitHub Desktop.
Save amir343/1292276 to your computer and use it in GitHub Desktop.
Parallel Experiment part one
object Data {
def apply(n:Int):List[Data] = {
val r = new Random
val lists = for { i <- 0 to n } yield new Data(r.nextInt(10000), r.nextInt(10000))
lists.toList
}
}
trait Computation {
def f(data:Data):Int = {
Thread.sleep(10)
data.a + data.b
}
def compute(list:List[Data]):Int
}
class Data(val a:Int = 0 , val b:Int = 0)
object ParallelExperiment {
def main(args: Array[String]) {
val list = Data(args(0).toInt)
var t1 = currentTimeMillis()
SequentialImplementation.compute(list)
var t2 = currentTimeMillis()
println("Sequential " + (t2-t1) + " (ms)")
t1 = currentTimeMillis()
ParallelImplementation.compute(list)
t2 = currentTimeMillis()
println("Parallel " + (t2-t1) + " (ms)")
}
}
object SequentialImplementation extends Computation {
def compute(list:List[Data]):Int = list.map{f}.max
}
object ParallelImplementation extends Computation {
def compute(list:List[Data]):Int = list.par.map{f}.par.max
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment