blob: 6f60cc43b211dcc46f6d131ab93491068bfd9e42 (
plain) (
blame)
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
|
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 }
|