summaryrefslogtreecommitdiff
path: root/scala/sizer.scala
diff options
context:
space:
mode:
Diffstat (limited to 'scala/sizer.scala')
-rw-r--r--scala/sizer.scala45
1 files changed, 45 insertions, 0 deletions
diff --git a/scala/sizer.scala b/scala/sizer.scala
new file mode 100644
index 0000000..6f60cc4
--- /dev/null
+++ b/scala/sizer.scala
@@ -0,0 +1,45 @@
+import scala.io._
+import scala.actors._
+import Actor._
+
+object PageLoader {
+ def getPageSize(url:String) = Source.fromURL(url, "ISO-8859-1").mkString.length
+}
+
+val urls = List("http://amazon.com",
+ "http://twitter.com",
+ "http://google.com",
+ "http://cnn.com")
+
+def timeMethod(method: () => Unit) = {
+ val start = System.nanoTime
+ method()
+ val end = System.nanoTime
+ println("Method 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 run:")
+timeMethod { getPageSizeSequentially }
+println("Concurrent run:")
+timeMethod { getPageSizeConcurrently }