Created
October 17, 2011 09:17
-
-
Save amir343/1292276 to your computer and use it in GitHub Desktop.
Parallel Experiment part one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Computation { | |
def f(data:Data):Int = { | |
Thread.sleep(10) | |
data.a + data.b | |
} | |
def compute(list:List[Data]):Int | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Data(val a:Int = 0 , val b:Int = 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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