Skip to content

Instantly share code, notes, and snippets.

@tjweir
Forked from derekjw/akka-future-pi.scala
Created April 11, 2011 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjweir/913588 to your computer and use it in GitHub Desktop.
Save tjweir/913588 to your computer and use it in GitHub Desktop.
import scalaz._,Scalaz._
import akka.scalaz.futures._
import akka.dispatch._
import System.{currentTimeMillis => now}
object Pi extends App {
val nrOfElements = 10000
val nrOfMessages = 10000
val startTime = now
// Single threaded and mutable
var piSeq = 0.0
0 until (nrOfMessages * nrOfElements) foreach (i =>
piSeq += 4 * math.pow(-1, i) / (2 * i + 1))
val seqTime = now
// Using Akka Future
val piFuture = (0 until nrOfMessages).foldLeft(Future(0.0))((result, n) =>
Future(((n * nrOfElements) until ((n + 1) * nrOfElements)).foldLeft(0.0)((acc, i) =>
acc + (4 * math.pow(-1, i) / (2 * i + 1)))) flatMap (acc => result map (_ + acc))).get
val futureTime = now
// Using Akka Future 2
val piFuture2 = (0 until nrOfMessages).foldLeft(Future(0.0))((result, n) =>
Future {
var acc = 0.0
((n * nrOfElements) until ((n + 1) * nrOfElements)).foreach(i =>
acc += (4 * math.pow(-1, i) / (2 * i + 1)))
acc
} flatMap (acc => result map (_ + acc))).get
val future2Time = now
// Using Akka Future and Scalaz
val piScalaz = (0 until nrOfMessages).foldMap(n =>
Future((n * nrOfElements) until ((n + 1) * nrOfElements) foldMap (i =>
4 * math.pow(-1, i) / (2 * i + 1)))).get
val scalazTime = now
// Parallel Collections
val piPar = (0 until nrOfMessages).par.map(n =>
((n * nrOfElements) until ((n + 1) * nrOfElements)).foldLeft(0.0)((acc, i) =>
acc + (4 * math.pow(-1, i) / (2 * i + 1)))).sum
val parTime = now
// Parallel Collections with a mutable var
val piPar2 = (0 until nrOfMessages).par.map { n =>
var acc = 0.0
((n * nrOfElements) until ((n + 1) * nrOfElements)).foreach(i =>
acc += (4 * math.pow(-1, i) / (2 * i + 1)))
acc
}.sum
println("\n\tSingle threaded with a mutable var")
println("\n\tPi estimate: \t\t%s\n\tCalculation time: \t%s millis".format(piSeq, (seqTime - startTime)))
println("\n\tAkka Future")
println("\n\tPi estimate: \t\t%s\n\tCalculation time: \t%s millis".format(piFuture, (futureTime - seqTime)))
println("\n\tAkka Future with a mutable var")
println("\n\tPi estimate: \t\t%s\n\tCalculation time: \t%s millis".format(piFuture2, (future2Time - futureTime)))
println("\n\tAkka Future and Scalaz")
println("\n\tPi estimate: \t\t%s\n\tCalculation time: \t%s millis".format(piScalaz, (scalazTime - future2Time)))
println("\n\tParallel Collections")
println("\n\tPi estimate: \t\t%s\n\tCalculation time: \t%s millis".format(piPar, (parTime - scalazTime)))
println("\n\tParallel Collections with a mutable var")
println("\n\tPi estimate: \t\t%s\n\tCalculation time: \t%s millis".format(piPar2, (now - parTime)))
println("\n\tPi actual: \t\t%s".format(scala.math.Pi))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment