Benchmarking in scala

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.


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.

链接地址: http://www.djcxy.com/p/86544.html

上一篇: 我在做什么错java 8 lambda Predicate <Integer>?

下一篇: 在scala中进行基准测试