Skip to content

Instantly share code, notes, and snippets.

@Atry
Created April 1, 2013 00:28
Show Gist options
  • Save Atry/5282605 to your computer and use it in GitHub Desktop.
Save Atry/5282605 to your computer and use it in GitHub Desktop.
Benchmark for scala.util.control.TailCalls
import org.junit._
class TailCallBenchmark {
import scala.util.continuations._
@Test
def test() {
import scala.util.control.TailCalls._
def unwind(): Unit @cps[TailRec[Unit]] = {
shift { (continue: Unit => TailRec[Unit]) =>
continue()
tailcall(continue())
}
}
def complexCpsFunction1(i: Int): Unit @cps[TailRec[Unit]] = {
if (i > 0) {
unwind()
unwind()
unwind()
unwind()
unwind()
unwind()
unwind()
unwind()
unwind()
complexCpsFunction1(0)
unwind()
unwind()
unwind()
unwind()
unwind()
complexCpsFunction1(0)
unwind()
unwind()
unwind()
unwind()
unwind()
unwind()
complexCpsFunction1(0)
unwind()
unwind()
unwind()
unwind()
unwind()
unwind()
complexCpsFunction1(0)
unwind()
unwind()
unwind()
unwind()
unwind()
unwind()
unwind()
unwind()
complexCpsFunction1(0)
unwind()
unwind()
unwind()
unwind()
unwind()
unwind()
complexCpsFunction1(i - 1)
}
}
def complexCpsFunction2(i: Int): Unit @cps[TailRec[Unit]] = {
if (i > 0) {
complexCpsFunction1(10)
complexCpsFunction2(i - 1)
}
}
def complexCpsFunction3(i: Int): Unit @cps[TailRec[Unit]] = {
if (i > 0) {
complexCpsFunction2(10)
complexCpsFunction3(i - 1)
}
}
for (j <- 0 until 10) {
System.gc()
val start = System.nanoTime()
for (i <- 0 until 1000) {
reset {
complexCpsFunction3(10)
shift { (continue: Unit => Unit) =>
done()
}
}.result
}
println("time:" + (System.nanoTime() - start))
}
System.gc()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment