public
Created — forked from derekjw/akka-future-pi.scala

  • Download Gist
akka-future-pi.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
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))
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.