Skip to content
Browse files

typing up the more real world actor example

  • Loading branch information...
1 parent 17f5df7 commit c4b72fdf2718845b80dae8e1cb09dac962c657e7 @joho committed Jan 3, 2011
Showing with 44 additions and 0 deletions.
  1. +44 −0 scala/sizer.scala
View
44 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.
Something went wrong with that request. Please try again.