Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benchmarking in scala [duplicate]

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.

like image 810
Maik Klein Avatar asked Oct 12 '12 00:10

Maik Klein


1 Answers

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.

like image 90
Luigi Plinge Avatar answered Oct 12 '22 23:10

Luigi Plinge