/javac-dottyc-scalac-bench

Scala is more rich than Java and must be slower to compile

Primary LanguageScala

Comparison of javac, scalac and dotc performance

"Scala is more rich as language than Java and must be slower to compile"

A comparison

A simple and fair comparison of compilation speed of javac, scalac and dotc on the source code that requires the same amount of work from all three compilers. The compiled Scala code is:

class A1 {
    def f1(x: String): String = x
    def f2(x: String): String = x
    .
    .
    def f100(x: String): String = x
}

and for Java:

class A1 {
    String f1(String x) { return x; }
    String f2(String x) { return x; }
    .
    .
    String f100(String x) { return x; }
}

For such simple code, Scala and Java coincide perfectly: the language features used are the same and the generated bytecode is the same.

Timing compilation of this simple code establishes a baseline. Both scalac, dotc and javac must perform the same amount of work to produce the output.

The chart

Running the benchmark yields this chart, though:

comparison chart

(on the x-axis is the number of generated classes and on y-axis is the number of seconds required to compile)

Let me start with the most astonishing result: dotc is a fresh rewrite of the Scala compiler and almost perfectly matches scalac's performance. For a fresh rewrite of that scale, to land at the same performance is remarkable outcome.

By plotting a sequence of measurements and comparing their slopes we can see the relative speed differences between compilers and mitigate fixed costs like startup time. Both scalac and dotc are 6x slower than javac.