From 32678cbe14590ee05feed1aa56aa33e2e7f2369f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ali=20Sinan=20K=C3=B6ksal?= <alisinan@gmail.com>
Date: Sun, 24 Apr 2011 15:25:37 +0000
Subject: [PATCH] Util class with timer.

---
 cp-demo/RedBlackTree.scala | 32 +++++++-------------------------
 cp-demo/SortedList.scala   | 20 ++++----------------
 src/cp/Utils.scala         | 17 +++++++++++++++++
 3 files changed, 28 insertions(+), 41 deletions(-)
 create mode 100644 src/cp/Utils.scala

diff --git a/cp-demo/RedBlackTree.scala b/cp-demo/RedBlackTree.scala
index 2f05ae891..b30df1da2 100644
--- a/cp-demo/RedBlackTree.scala
+++ b/cp-demo/RedBlackTree.scala
@@ -1,4 +1,5 @@
 import cp.Definitions._
+import cp.Utils._
 
 object RedBlackTree { 
   @spec sealed abstract class Color
@@ -85,20 +86,23 @@ object RedBlackTree {
 
     // enumerateAllUpTo(bound)
 
+    /*
     try {
       val t = choose((t: Tree) => size(t) > 7 && height(t) <= 3)
     } catch {
       case e: UnsatisfiableConstraintException => println("constraint is unsatisfiable")
     }
+    */
 
     val solutionSet = scala.collection.mutable.Set[Tree]()
     println("Fixing size of trees to " + (bound))
-    Timer.go
+    val timer = new Timer("Fixed-size enumeration", true)
+    timer.start
     for (tree <- findAll((t : Tree) => isRedBlackTree(t) && boundValues(t, bound - 1) && size(t) == bound)) {
       solutionSet += tree
     }
-    Timer.stop
-    
+    timer.stop
+
     // for (tree <- solutionSet)
     //   println(print(tree) + "\n-----------\n")
     println("Fixed-size solution set size : " + solutionSet.size)
@@ -113,32 +117,24 @@ object RedBlackTree {
     val set4 = scala.collection.mutable.Set[Tree]()
 
     println("Minimizing size:")
-    Timer.go
     for (tree <- findAll((t : Tree) => isRedBlackTree(t) && boundValues(t, bound) minimizing size(t))) {
       set1 += tree
     }
-    Timer.stop
     
     println("Minimizing height:")
-    Timer.go
     for (tree <- findAll((t : Tree) => isRedBlackTree(t) && boundValues(t, bound) minimizing height(t))) {
       set2 += tree
     }
-    Timer.stop
 
     println("Minimizing bound:")
-    Timer.go
     for ((tree, bb) <- findAll((t : Tree, b: Int) => isRedBlackTree(t) && boundValues(t, b) && b >= 0 && b <= bound minimizing b)) {
       set3 += tree
     }
-    Timer.stop
     
     println("No minimization:")
-    Timer.go
     for (tree <- findAll((t : Tree) => isRedBlackTree(t) && boundValues(t, bound))) {
       set4 += tree
     }
-    Timer.stop
 
     println("Solution set size: " + set1.size)
     assert(set1 == set2)
@@ -155,17 +151,3 @@ object RedBlackTree {
     case Empty() => "E"
   }
 }
-
-object Timer {
-  var start: Long = 0L
-  var end: Long = 0L
-  def go = {
-    start = System.currentTimeMillis
-  }
-  def stop : Double = {
-    end = System.currentTimeMillis
-    val seconds = (end - start) / 1000.0
-    println("  Measured time: " + seconds + " s")
-    seconds
-  }
-}
diff --git a/cp-demo/SortedList.scala b/cp-demo/SortedList.scala
index 88179bb35..5e5ef22dc 100644
--- a/cp-demo/SortedList.scala
+++ b/cp-demo/SortedList.scala
@@ -1,4 +1,5 @@
 import cp.Definitions._
+import cp.Utils.Timer
 
 object Lists {
     @spec sealed abstract class List
@@ -29,25 +30,12 @@ object SortedList {
     val len = if (args.isEmpty) 3 else args(0).toInt
     val set = scala.collection.mutable.Set[List]()
 
-    Timer.go
+    val timer = new Timer("Sorted list enumeration", true)
+    timer.start
     for (list <- findAll((l : List) => isSorted(l) && valuesWithin(l, 0, len) && size(l) == len))
       set += list
-    Timer.stop
+    timer.stop
       
     println("size : " + set.size)
   }
 }
-
-object Timer {
-  var start: Long = 0L
-  var end: Long = 0L
-  def go = {
-    start = System.currentTimeMillis
-  }
-  def stop : Double = {
-    end = System.currentTimeMillis
-    val seconds = (end - start) / 1000.0
-    println("  Measured time: " + seconds + " s")
-    seconds
-  }
-}
diff --git a/src/cp/Utils.scala b/src/cp/Utils.scala
new file mode 100644
index 000000000..2eb9b5971
--- /dev/null
+++ b/src/cp/Utils.scala
@@ -0,0 +1,17 @@
+package cp
+
+object Utils {
+  class Timer(description : String, verbose : Boolean = false) {
+    var beginning: Long = 0L
+    var end: Long = 0L
+    def start = {
+      beginning = System.currentTimeMillis
+    }
+    def stop : Double = {
+      end = System.currentTimeMillis
+      val seconds = (end - beginning) / 1000.0
+      if (verbose) println("Timer \"" + description + "\": " + seconds + " s")
+      seconds
+    }
+  }
+}
-- 
GitLab