Search code examples
scalaexecution-time

using scastie and scalafiddle to evaluate code execution time


I'm trying to reduce execution time of some small scala routine, say, concatenation of strings, since I'm too lazy to setup local environment, I'm using online scala compilers, but found the comparison result differs between scastie and scalafiddle w/ the following code:

// routine 1
var startT1 = System.nanoTime()
(1 until 100 * 1000).foreach{ x=>
  val sb = new StringBuilder("a")
  sb.append("b").append("c").append("d").append("e").append("f")
}
println(System.nanoTime() - startT1)

// routine 2
var startT2 = System.nanoTime()
(1 until 100 * 1000).foreach{ x=>
  val arr = Array[Char]('a', 'b', 'c', 'd', 'e', 'f')
}
println(System.nanoTime() - startT2)

In scalafiddle routine 1 is faster but in scastie routine 2 is faster.
I'v read this article https://medium.com/@otto.chrons/what-makes-scalafiddle-so-fast-9a3edf33ed4d, so it seems that scalafiddle actually runs JavaScript instead of scala. But the remaining question is, can I really use scastie for execution time benchmarks?


Solution

  • Short answer - NO I don't you can not rely on ANY online running tools like scastie and scalafiddle to verify performance.

    Because, there is 1000 and more reasons why some benchmark will show X millis execution for some operation, and 99% of that reasons is running environment: hardware, operating system, CPU architecture, load on machine, used Scala compiler, used JVM etc. And we don't know environment change between runs on Scatie for instance, so you can get totally different numbers and don't know why, hence benchmark results won't be reliable.

    If you would like to get some results, you would like rely one at least a bit, take a look at https://openjdk.java.net/projects/code-tools/jmh/ and it's sbt helper plugin https://github.com/ktoso/sbt-jmh and run known environment.

    And along with posting benchmark results - please post environment details, where it was run.