[Assorted-commits] SF.net SVN: assorted:[1455] sandbox/trunk/src/scala
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-07-13 06:27:28
|
Revision: 1455 http://assorted.svn.sourceforge.net/assorted/?rev=1455&view=rev Author: yangzhang Date: 2009-07-13 06:27:22 +0000 (Mon, 13 Jul 2009) Log Message: ----------- added some old microbenchmarks Added Paths: ----------- sandbox/trunk/src/scala/bench/ sandbox/trunk/src/scala/bench/StreamBench.scala sandbox/trunk/src/scala/bench/StreamRecursion.scala sandbox/trunk/src/scala/bench/TextPerf.scala Added: sandbox/trunk/src/scala/bench/StreamBench.scala =================================================================== --- sandbox/trunk/src/scala/bench/StreamBench.scala (rev 0) +++ sandbox/trunk/src/scala/bench/StreamBench.scala 2009-07-13 06:27:22 UTC (rev 1455) @@ -0,0 +1,161 @@ +package scalatest; + +object StreamBench { + + def time[a](label: String)(f: => a) = { + val start = System.currentTimeMillis + val result = try { f } finally { + val end = System.currentTimeMillis + println("time for " + label + ": " + (end - start) + "ms") + } + result + } + + def main(args: Array[String]) { + randomAccess + } + + def multitime(label: String)(f: => Any) { + time(label)(f) + time(label)(f) + time(label)(f) + } + + def randomAccess { + val (step,stop) = (100000,900000) + + // stack overflow + /* + val xs = List range (0, stop); + multitime("random access into list") { + xs(stop-1) + } + */ + + multitime("random access into stream") { + Stream range (0, stop) apply (stop-1) + } + + val s = Stream range (0, stop) + s(stop-1) + multitime("random access into cached stream") { + s(stop-1) + } + } + + def cons { + val (step,stop) = (100000,900000) + + // Array.apply calls Stream.length, causing a stack overflow + /* + multitime("array via stream") { + def r(i: Int): Stream[Int] = + if (i == stop) Stream empty + else Stream cons (i, r(i+1)) + Array(r(0):_*) + } + */ + + multitime("iterating into array") { + val a = new Array[Int](stop) + Iterator range (0, stop) copyToArray (a,0) + a + } + + multitime("iterating into boxed array") { + val a = new Array[Integer](stop) + Iterator range (0, stop) map (x => new Integer(x)) copyToArray (a,0) + a + } + + multitime("iterating into arraybuffer") { + val a = new scala.collection.mutable.ArrayBuffer[Int] + Iterator range (0, stop) copyToBuffer a + a + } + + // resizeablearray starts at 16 and doubles + multitime("iterating into arraybuffer with capacity") { + val a = new scala.collection.mutable.ArrayBuffer[Int] { + ensureSize(stop+10) + } + Iterator range (0, stop) copyToBuffer a + a + } + + multitime("iterating into listbuffer") { + val a = new scala.collection.mutable.ListBuffer[Int] + Iterator range (0, stop) copyToBuffer a + a + } + } + + def streams { + val (step,stop) = (100000,900000) + + time("for-loop") { + var i = 0 + while (i < stop) { + if ( i % step == 0 ) println(i) + i += 1 + } + } + + time("traversing Iterator") { + def f { + for ( i <- Iterator from 0 ) { + if ( i % step == 0 ) { + if ( i > stop ) return + println(i) + } + } + } + f + } + + def traverseStream(s: Stream[Int]) { + val i = s.head + if ( i % step == 0 ) { + if ( i > stop ) return + println( i ) + } + traverseStream( s.tail ) + } + + time ( "traversing Stream" ) { + traverseStream( Stream from 0 ) + } + + // explicitly putting this in a separate method allows s to be GCed + def g { + val s = Stream from 0 + time ( "caching Stream" ) { + traverseStream( s ) + } + + time ( "traversing cached Stream" ) { + traverseStream( s ) + } + } + g + g // goes much faster the second time + + time ( "Stream.foreach" ) { + def f { + Stream from 0 foreach { i => + if ( i % step == 0 ) { + if ( i > stop ) return + println( i ) + } + } + } + f + } + } + + def mkStream(iter: Iterator[Int]): Stream[Int] = { + if (!iter.hasNext) Stream empty + else Stream cons (iter.next, mkStream(iter)) + } + +} Added: sandbox/trunk/src/scala/bench/StreamRecursion.scala =================================================================== --- sandbox/trunk/src/scala/bench/StreamRecursion.scala (rev 0) +++ sandbox/trunk/src/scala/bench/StreamRecursion.scala 2009-07-13 06:27:22 UTC (rev 1455) @@ -0,0 +1,19 @@ +package scalatest; + +import commons.Collections._ + +object StreamRecursion { + + def main(args: Array[String]) { + val s = Stream range (0, 100000) + def f(s: Stream[Int]): Stream[Int] = + Stream cons ( + s.head, + f(Stream fromIterator (s drop 1 elements)) + ) + f(s) foreach ( x => + if (x % 10000 == 0) println(x) + ) + } + +} Added: sandbox/trunk/src/scala/bench/TextPerf.scala =================================================================== --- sandbox/trunk/src/scala/bench/TextPerf.scala (rev 0) +++ sandbox/trunk/src/scala/bench/TextPerf.scala 2009-07-13 06:27:22 UTC (rev 1455) @@ -0,0 +1,90 @@ +package scalatest; + +object TextPerf { + def time[a](label: String)(f: => a) = { + val start = System.currentTimeMillis + val result = try { f } finally { + val end = System.currentTimeMillis + println("time for " + label + ": " + (end - start) + "ms") + } + result + } + def multitime(label: String)(f: => Any) { + time(label)(f) + time(label)(f) + time(label)(f) + } + def main(args: Array[String]) { + val count = 100000 + println("abc" replaceAll ("b","")) + multitime("replaceAll (inline)") { + 0 until count foreach { x => + "abc" replaceAll ("b","") + } + } + multitime("replaceAll (named function)") { + def f = "abc" replaceAll ("b","") + 0 until count foreach { x => + f + } + } + multitime("replaceAll (parameterized function)") { + def f(s:String) = s replaceAll ("b","") + 0 until count foreach { x => + f("abc") + } + } + + def f(s:String) = s filter (_!='b') mkString "" + println(f("abc")) + multitime("filter") { + 0 until count foreach { x => f("abc") } + } + + val p = java.util.regex.Pattern compile "b" + def g(s:String) = { + p matcher s replaceAll "" + } + println(g("abc")) + multitime("compiled") { + 0 until count foreach { x => g("abc") } + } + + def h(s:Array[Char]) = { + val t = new Array[Char](3) + var i, j = 0 + while (i < s.length) { + if (s(i) != 'b') { + t(j) = s(i) + j += 1 + } + i += 1 + } + t + } + def i(ss:String) = { + val s = ss.toCharArray + val t = new Array[Char](s.length) + var i, j = 0 + while (i < s.length) { + if (s(i) != 'b') { + t(j) = s(i) + j += 1 + } + i += 1 + } + new String(t, 0, j) + } + val s = "abc" toArray; + println(new String(h("abc" toCharArray))) + multitime("array") { + 0 until count foreach { x => h(s) } + } + multitime("array conversion + array") { + 0 until count foreach { x => h("abc" toCharArray) } + } + multitime("array conversion + array + string conversion") { + 0 until count foreach { x => i("abc") } + } + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |