Skip to content

Commit

Permalink
typing up the more real world actor example
Browse files Browse the repository at this point in the history
  • Loading branch information
joho committed Jan 3, 2011
1 parent 17f5df7 commit c4b72fd
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions scala/sizer.scala
@@ -0,0 +1,44 @@
import scala.io._
import scala.actors._
import Actor._

object PageLoader {
def getPageSize(url : String) = Source.fromURL(url).mkString.length
}

val urls = List("http://twitter.com",
"http://amazon.com",
"http://themeforest.net")

def timeMethod(method: () => Unit) = {
val start = System.nanoTime
method()
val end = System.nanoTime
println("Mthod took " + (end - start)/1000000000.0 + "seconds.")
}

def getPageSizeSequentially() = {
for(url <- urls) {
println("Size for " + url + ": " + PageLoader.getPageSize(url))
}
}

def getPageSizeConcurrently() = {
val caller = self

for(url <- urls) {
actor { caller ! (url, PageLoader.getPageSize(url)) }
}

for(i <- 1 to urls.size) {
receive {
case (url, size) => println("Size for " + url + ": " + size)
}
}
}

println("Sequential")
timeMethod { getPageSizeSequentially }
println("Concurrent")
timeMethod { getPageSizeConcurrently }

0 comments on commit c4b72fd

Please sign in to comment.