Benchmarking in scala

benchmarking, scala

Solution

The quick way is to use `testing.Benchmark`: you just write something like

object Bench extends testing.Benchmark {
  // initialize your data here
  def run() {
    // code to benchmark here
  }
}

which you run with something like `scala Bench 5 1000000` (or directly from your IDE, editing the run configuration), which gives you timings for 5 sets of 1000000 repetitions of the `run()` method. You can compare timings for each set and check it has become consistent (the first set is usually slower due to JVM warm-up).

For a more rigorous approach using Caliper, a Java microbenchmarking framework, see this blog post: http://www.decodified.com/scala/2011/04/19/microbenchmarking-scala-code.

Problem

Possible Duplicate: How do I write a correct micro-benchmark in Java? Let's say I have two algorithms, how do I find out which one has a higher performance? I mean i can proof it mathematically but if I use some libraries this can get tedious. I never learned how I do correct benchmarks without math.

Original source

Related problems