299 time_needed(10, for (n <- list) yield n + 42)  | 
   299 time_needed(10, for (n <- list) yield n + 42)  | 
   300 time_needed(10, for (n <- list.par) yield n + 42)  | 
   300 time_needed(10, for (n <- list.par) yield n + 42)  | 
   301   | 
   301   | 
   302   | 
   302   | 
   303   | 
   303   | 
   304 // Problems with mutability and parallel computations  | 
         | 
   305 //====================================================  | 
         | 
   306   | 
         | 
   307 def count_intersection(A: Set[Int], B: Set[Int]) : Int = { | 
         | 
   308   var count = 0  | 
         | 
   309   for (x <- A; if (B contains x)) count += 1   | 
         | 
   310   count  | 
         | 
   311 }  | 
         | 
   312   | 
         | 
   313 val A = (1 to 1000).toSet  | 
         | 
   314 val B = (1 to 1000 by 4).toSet  | 
         | 
   315   | 
         | 
   316 count_intersection(A, B)  | 
         | 
   317   | 
         | 
   318 // but do not try to add .par to the for-loop above  | 
         | 
   319   | 
         | 
   320   | 
         | 
   321 //propper parallel version  | 
         | 
   322 def count_intersection2(A: Set[Int], B: Set[Int]) : Int =   | 
         | 
   323 	A.par.count(x => B contains x)  | 
         | 
   324   | 
         | 
   325 count_intersection2(A, B)  | 
         | 
   326   | 
         | 
   327   | 
         | 
   328 //for measuring time  | 
         | 
   329 def time_needed[T](n: Int, code: => T) = { | 
         | 
   330   val start = System.nanoTime()  | 
         | 
   331   for (i <- (0 to n)) code  | 
         | 
   332   val end = System.nanoTime()  | 
         | 
   333   (end - start) / 1.0e9  | 
         | 
   334 }  | 
         | 
   335   | 
         | 
   336 val A = (1 to 1000000).toSet  | 
         | 
   337 val B = (1 to 1000000 by 4).toSet  | 
         | 
   338   | 
         | 
   339 time_needed(10, count_intersection(A, B))  | 
         | 
   340 time_needed(10, count_intersection2(A, B))  | 
         | 
   341   | 
         | 
   342   | 
   304   | 
   343   | 
   305   | 
   344   | 
   306   | 
   345 // Webpages  | 
   307 // Webpages  | 
   346 //==========  | 
   308 //==========  |