From d67d3c2ebad1a373c6e8abb7603394f8d7444e1a Mon Sep 17 00:00:00 2001 From: Iulian Dragos Date: Thu, 21 Oct 2004 09:48:58 +0000 Subject: Scala benchmark added. --- test/benchmark/sources/parsers/build.xml | 37 ++++++++ test/benchmark/sources/parsers/parsers.scala | 124 +++++++++++++++++++++++++ test/benchmark/sources/sort1/build.xml | 36 +++++++ test/benchmark/sources/sort1/sort1.scala | 28 ++++++ test/benchmark/sources/sort2/build.xml | 36 +++++++ test/benchmark/sources/sort2/sort2.scala | 35 +++++++ test/benchmark/sources/viewtest/build.xml | 36 +++++++ test/benchmark/sources/viewtest/viewtest.scala | 52 +++++++++++ 8 files changed, 384 insertions(+) create mode 100644 test/benchmark/sources/parsers/build.xml create mode 100644 test/benchmark/sources/parsers/parsers.scala create mode 100644 test/benchmark/sources/sort1/build.xml create mode 100644 test/benchmark/sources/sort1/sort1.scala create mode 100644 test/benchmark/sources/sort2/build.xml create mode 100644 test/benchmark/sources/sort2/sort2.scala create mode 100644 test/benchmark/sources/viewtest/build.xml create mode 100755 test/benchmark/sources/viewtest/viewtest.scala (limited to 'test') diff --git a/test/benchmark/sources/parsers/build.xml b/test/benchmark/sources/parsers/build.xml new file mode 100644 index 0000000000..9217ba443e --- /dev/null +++ b/test/benchmark/sources/parsers/build.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/benchmark/sources/parsers/parsers.scala b/test/benchmark/sources/parsers/parsers.scala new file mode 100644 index 0000000000..0089982eab --- /dev/null +++ b/test/benchmark/sources/parsers/parsers.scala @@ -0,0 +1,124 @@ +/* __ *\ +** ________ ___ / / ___ Scala benchmark suite ** +** / __/ __// _ | / / / _ | (c) 2003-2004, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ + +package benchmarks; + +/** The parsers example from the scala distribution. */ +abstract class Parsers { + + type intype; + + abstract class Parser { + + type Result = Option[intype]; + + def apply(in: intype): Result; + + /*** p &&& q applies first p, and if that succeeds, then q + */ + def &&& (def q: Parser) = new Parser { + def apply(in: intype): Result = Parser.this.apply(in) match { + case None => None + case Some(in1) => q(in1) + } + } + + /*** p ||| q applies first p, and, if that fails, then q. + */ + def ||| (def q: Parser) = new Parser { + def apply(in: intype): Result = Parser.this.apply(in) match { + case None => q(in) + case s => s + } + } + } + + val empty = new Parser { + def apply(in: intype): Result = Some(in) + } + + val fail = new Parser { + def apply(in: intype): Result = None + } + + def opt(p: Parser): Parser = p ||| empty; // p? = (p | ) + def rep(p: Parser): Parser = opt(rep1(p)); // p* = [p+] + def rep1(p: Parser): Parser = p &&& rep(p); // p+ = p p* +} + +abstract class ListParsers extends Parsers { + + def chr(p: char => boolean): Parser; + + def chr(c: char): Parser = chr(d: char => d == c); + + def letter : Parser = chr(Character.isLetter); + def digit : Parser = chr(Character.isDigit); + + def ident : Parser = letter &&& rep(letter ||| digit); + def number : Parser = digit &&& rep(digit); + def list : Parser = chr('(') &&& listElems &&& chr(')'); + def listElems : Parser = expr &&& (chr(',') &&& listElems ||| empty); + def expr : Parser = ident ||| number ||| list; +} + +abstract class ExprParsers extends Parsers { + def chr(p: char => boolean): Parser; + def chr(c: char): Parser = chr(d: char => d == c); + + def digit : Parser = chr(Character.isDigit); + def number : Parser = digit &&& rep(digit); + def summand : Parser = number ||| chr('(') &&& expr &&& chr(')'); + def expr : Parser = summand &&& rep(chr('+') &&& summand) +} + +class ParseString(s: String) extends Parsers { + type intype = int; + val input = 0; + def chr(p: char => boolean) = new Parser { + def apply(in: int): Parser#Result = + if (in < s.length() && p(s charAt in)) Some(in + 1); + else None; + } +} + +object TestList { + + def main(args: Array[String]): unit = + if (args.length == 1) { + val ps = new ListParsers with ParseString(args(0)); + ps.expr(ps.input) match { + case Some(n) => + System.out.println("parsed: " + args(0).substring(0, n)); + case None => + System.out.println("nothing parsed"); + } + } else System.out.println("usage: java examples.TestList "); +} + +object TestExpr { + + def main(args: Array[String]): unit = + if (args.length == 1) { + val ps = new ExprParsers with ParseString(args(0)); + ps.expr(ps.input) match { + case Some(n) => + System.out.println("parsed: " + args(0).substring(0, n)); + case None => + System.out.println("nothing parsed"); + } + } else System.out.println("usage: java examples.TestExpr "); +} + +object parsers1 with scala.testing.Benchmark { + def run: Unit = { + TestList.main(Array("(a,b,(1,2))")); + TestExpr.main(Array("2+3+(4+1)")) + } +} diff --git a/test/benchmark/sources/sort1/build.xml b/test/benchmark/sources/sort1/build.xml new file mode 100644 index 0000000000..31f48144cf --- /dev/null +++ b/test/benchmark/sources/sort1/build.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/benchmark/sources/sort1/sort1.scala b/test/benchmark/sources/sort1/sort1.scala new file mode 100644 index 0000000000..6795158a78 --- /dev/null +++ b/test/benchmark/sources/sort1/sort1.scala @@ -0,0 +1,28 @@ +/* __ *\ +** ________ ___ / / ___ Scala benchmark suite ** +** / __/ __// _ | / / / _ | (c) 2003-2004, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ + + +package benchmarks; + +/** Quick sort with a functional taste. */ +object sort1 with scala.testing.Benchmark { + + def sort(a: List[Int]): List[Int] = { + if (a.length < 2) + a + else { + val pivot = a(a.length / 2); + sort(a.filter(x => x < pivot)) + ::: a.filter(x => x == pivot) + ::: sort(a.filter(x => x > pivot)) + } + } + + def run: Unit = sort(List.range(1, 10000)); +} diff --git a/test/benchmark/sources/sort2/build.xml b/test/benchmark/sources/sort2/build.xml new file mode 100644 index 0000000000..067b98afb6 --- /dev/null +++ b/test/benchmark/sources/sort2/build.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/benchmark/sources/sort2/sort2.scala b/test/benchmark/sources/sort2/sort2.scala new file mode 100644 index 0000000000..6c024eab7a --- /dev/null +++ b/test/benchmark/sources/sort2/sort2.scala @@ -0,0 +1,35 @@ +/* __ *\ +** ________ ___ / / ___ Scala benchmark suite ** +** / __/ __// _ | / / / _ | (c) 2003-2004, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ + +package benchmarks; + +/** Quick-sort a list of integers, version 2. Taken from the + Scala distribution examples. */ +class Sorter { + + def sort(a: List[Int]): List[Int] = { + if (a.length < 2) + a + else { + val pivot = a(a.length / 2); + def lePivot(x: Int) = x < pivot; + def gtPivot(x: Int) = x > pivot; + def eqPivot(x: Int) = x == pivot; + sort(a filter lePivot) + ::: sort(a filter eqPivot) + ::: sort(a filter gtPivot) + } + } + +} + +object sort2 extends Sorter with scala.testing.Benchmark { + def run: Unit = sort(List.range(1,10000)); + +} diff --git a/test/benchmark/sources/viewtest/build.xml b/test/benchmark/sources/viewtest/build.xml new file mode 100644 index 0000000000..0816f75a18 --- /dev/null +++ b/test/benchmark/sources/viewtest/build.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/benchmark/sources/viewtest/viewtest.scala b/test/benchmark/sources/viewtest/viewtest.scala new file mode 100755 index 0000000000..82807808bd --- /dev/null +++ b/test/benchmark/sources/viewtest/viewtest.scala @@ -0,0 +1,52 @@ +/* __ *\ +** ________ ___ / / ___ Scala benchmark suite ** +** / __/ __// _ | / / / _ | (c) 2003-2004, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ + +package benchmarks; + +/* View test class from the scala test suite */ + +trait Ordered[a] { + def < (x: a): boolean; +} + +object O { + def view (x: String): Ordered[String] = new Ordered[String] { + def < (y: String) = x.compareTo(y) < 0; + } +} + +object Empty extends Tree[All]; +case class Node[+c <% Ordered[c]](elem: c, l: Tree[c], r: Tree[c]) extends Tree[c]; + +trait Tree[+a <% Ordered[a]] { + def insert[b >: a <% Ordered[b]](x: b): Tree[b] = this match { + case Empty => new Node(x, Empty, Empty) + case Node(elem, l, r) => + if (x == elem) this + else if (x < elem) Node(elem, l insert x, r) + else Node(elem, l, r insert x); + } + def elements: List[a] = this match { + case Empty => List() + case Node(elem, l, r) => + l.elements ::: List(elem) ::: r.elements + } +} + +object viewtest1 with scala.testing.Benchmark { + import O.view; + + def run: Unit = { + var t: Tree[String] = Empty; + val args = List("1", "2", "3", "4", "5", "7", "8", "9", "10"); + for (val s <- args) { + t = t insert s + } + } +} -- cgit v1.2.3