progs/scala/re-simp.scala
changeset 158 4e00dd2398ac
parent 157 1fe44fb6d0a4
child 166 cab1ae6f339a
--- a/progs/scala/re-simp.scala	Thu Mar 31 16:50:37 2016 +0100
+++ b/progs/scala/re-simp.scala	Fri Apr 01 16:29:33 2016 +0100
@@ -69,6 +69,7 @@
 }
 
 // derivative w.r.t. a string (iterates der)
+@tailrec
 def ders (s: List[Char], r: Rexp) : Rexp = s match {
   case Nil => r
   case c::s => ders(s, der(c, r))
@@ -191,14 +192,14 @@
 // Some Tests
 //============
 
-def time[T](code: => T) = {
+def time_needed[T](i: Int, code: => T) = {
   val start = System.nanoTime()
-  val result = code
+  for (j <- 1 to i) code
   val end = System.nanoTime()
-  println((end - start)/1.0e9)
-  result
+  (end - start)/(i * 1.0e9)
 }
 
+
 val r0 = ("a" | "ab") ~ ("b" | "")
 println(lexing(r0, "ab"))
 println(lexing_simp(r0, "ab"))
@@ -266,8 +267,8 @@
 println(env(lexing_simp(WHILE_REGS, prog1)))
 
 
-// Big Test
-//==========
+// Bigger Test
+//=============
 val prog2 = """
 i := 2;
 max := 100;
@@ -311,3 +312,17 @@
 }
 */
 
+// Sulzmann's tests
+//==================
+
+val sulzmann = ("a" | "b" | "ab").%
+
+println(lexing_simp(sulzmann, "a" * 10))
+
+for (i <- 1 to 4501 by 500) {
+  println(i + ": " + "%.5f".format(time_needed(1, lexing_simp(sulzmann, "a" * i))))
+}
+
+for (i <- 1 to 2001 by 500) {
+  println(i + ": " + "%.5f".format(time_needed(1, lexing_simp(sulzmann, "ab" * i))))
+}