Advertisement
Guest User

Run with args 5 10000

a guest
Dec 19th, 2012
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. object test extends testing.Benchmark {
  3.  
  4.   case class Row(time: Int)
  5.  
  6.   def counts(xs: List[Row], f : Row => Boolean, complete: Int = 0, incomplete: Int = 0): (Int, Int) =
  7.     xs match {
  8.       case Nil => (complete, incomplete)
  9.       case row :: tail =>
  10.         if (f(row)) counts(tail, f, complete + 1, incomplete)
  11.         else counts(tail, f, complete, incomplete + 1)
  12.     }
  13.  
  14.   def countsdhg(xs: List[Row], f : Row => Boolean) = {
  15.     val complete = xs.count(f(_))
  16.     (complete, xs.size - complete)
  17.   }
  18.  
  19.   def countsdhg2(xs: List[Row], f : Row => Boolean) = {
  20.     val (complete, incomplete) = xs.iterator.partition(f(_))
  21.     (complete.size, incomplete.size)
  22.   }
  23.  
  24.   def countvars(xs: List[Row], f : Row => Boolean) = {
  25.     var complete = 0;
  26.     var incomplete = 0;
  27.     xs.foreach(r => if (f(r)) complete += 1 else incomplete += 1)
  28.     (complete, incomplete)
  29.   }
  30.  
  31.   def countdg(xs: List[Row], f: Row => Boolean) = {
  32.     val complete = xs.view.filter(f(_)).size
  33.     (complete, xs.length - complete)
  34.   }
  35.  
  36.   val xs = List.fill(5000)(Row(util.Random.nextInt(2)))
  37.  
  38.   var res = (0, 0) // to avoid method call being optimized away
  39.  
  40.   def run {
  41.     def f(r : Row) = r.time == 0
  42.     //res = counts(xs, f)     //  720
  43.     //res = countsdhg(xs, f)  //   730
  44.     //res = countsdhg2(xs, f) //  2780
  45.     res = countvars(xs, f)  //   486
  46.     //res = countdg(xs, f)     // more than 270,000!
  47.   }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement