scala/ex.scala
changeset 221 18905d086cbb
parent 210 5e2e576fac7c
child 222 d682591c63e1
equal deleted inserted replaced
220:262fc2c6371b 221:18905d086cbb
    88 
    88 
    89 // compilation of rec to abacus tests
    89 // compilation of rec to abacus tests
    90 def test_comp2(f: Rec, ns: Int*) = {
    90 def test_comp2(f: Rec, ns: Int*) = {
    91   val (abc_f, arity, _) = compile_rec(f)
    91   val (abc_f, arity, _) = compile_rec(f)
    92   val abc_map = (0 until ns.length).zip(ns).toMap[Int, Int]
    92   val abc_map = (0 until ns.length).zip(ns).toMap[Int, Int]
       
    93   val start = System.nanoTime()
    93   val res = (abc_f.run(abc_map))(arity)
    94   val res = (abc_f.run(abc_map))(arity)
    94   ("Result: " + res + "  length: " + abc_f.p.length)
    95   val end = System.nanoTime()
       
    96   val time = (end - start)/1.0e9
       
    97   ("Result: " + res + "  length: " + abc_f.p.length + " time: " + "%.5f".format(time))
    95 }
    98 }
    96 
    99 
    97 println("S(3)          " + test_comp2(S, 3))
   100 println("S(3)          " + test_comp2(S, 3))
    98 println("Const(1)      " + test_comp2(Const(1), 0))
   101 println("Const(1)      " + test_comp2(Const(1), 0))
    99 println("Const(10)     " + test_comp2(Const(10), 0))
   102 println("Const(10)     " + test_comp2(Const(10), 0))
   100 println("Add(69, 30)   " + test_comp2(Add, 69, 30))
   103 println("Add(69, 30)   " + test_comp2(Add, 69, 30))
   101 println("Mult(13, 9)   " + test_comp2(recs.Mult, 13, 9))
   104 println("Mult(13, 9)   " + test_comp2(recs.Mult, 13, 9))
   102 println("Power(3, 4)   " + test_comp2(Power, 3, 4))
   105 println("Power(3, 4)   " + test_comp2(Power, 3, 4))
   103 println("Minus(30, 4)  " + test_comp2(Minus, 30, 4))
   106 println("Minus(30, 4)  " + test_comp2(Minus, 30, 4))
   104 println("Fact(5)       " + test_comp2(Fact, 5))
   107 println("Fact(5)       " + test_comp2(Fact, 5))
   105 //println("Prime(5)      " + test_comp2(Prime, 5))
   108 println("Prime(5)      " + test_comp2(Prime, 5))
   106 //println("Prime(4)      " + test_comp2(Prime, 4))
   109 //println("Prime(4)      " + test_comp2(Prime, 4))
   107 println("Strt(1)(2)    " + test_comp2(Strt(1), 2))
   110 println("Strt(1)(2)    " + test_comp2(Strt(1), 2))
   108 
   111 
   109 
   112 
   110 def test_comp1(f: Rec, ns: Int*) = {
   113 def test_comp1(f: Rec, ns: Int*) = {
   111   val (abc_f, arity, _) = compile_rec(f)
   114   val (abc_f, arity, _) = compile_rec(f)
   112   val tm = toTM(abc_f.p) :+ TMMopup(arity)
   115   val tm = toTM(abc_f.p) :+ TMMopup(arity)
       
   116   val start = System.nanoTime()
   113   val res = tm.run(Tape(ns.toList))
   117   val res = tm.run(Tape(ns.toList))
   114   ("length: " + tm.p.length + " tape: " + arity + "\nResult: " + res)
   118   val end = System.nanoTime()
       
   119   val time = (end - start)/1.0e9
       
   120   ("length: " + tm.p.length + " tape: " + arity + " time: " + "%.5f".format(time) + "\nResult: " + res)
   115 }
   121 }
   116 
   122 
   117 println("")
   123 println("")
   118 println("S(3)          " + test_comp1(S, 3))
   124 println("S(3)          " + test_comp1(S, 3))
   119 println("Const(10)     " + test_comp1(Const(10), 0))
   125 println("Const(10)     " + test_comp1(Const(10), 0))