Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java compile speed vs Scala compile speed

I've been programming in Scala for a while and I like it but one thing I'm annoyed by is the time it takes to compile programs. It's seems like a small thing but with Java I could make small changes to my program, click the run button in netbeans, and BOOM, it's running, and over time compiling in scala seems to consume a lot of time. I hear that with many large projects a scripting language becomes very important because of the time compiling takes, a need that I didn't see arising when I was using Java.

But I'm coming from Java which as I understand it, is faster than any other compiled language, and is fast because of the reasons I switched to Scala(It's a very simple language).

So I wanted to ask, can I make Scala compile faster and will scalac ever be as fast as javac.

like image 344
user405163 Avatar asked Aug 16 '10 03:08

user405163


People also ask

Is Scala as fast as Java?

Scala vs Java performance Some coders claim that Scala is a little bit faster than Java programming with 20% fast processing. Optimization in Scala language makes code compilation much faster than Java coding.

Why does Scala take so long to compile?

First things first: compilation is slow because the language is rich with features. Of course, there are examples where languages with approximately the same set of features compile faster, but we have what we have.

Does Java compile fast?

The result in this scenario is that Java build times have an average of 15.5 seconds, while Kotlin averages 18.5 seconds: an increase of 17%. Kotlin is not off to a great start, but this isn't how most people will compile their code. For clean builds with no Gradle daemon, Java compiles 17% faster than Kotlin.

Why is Scala faster?

Both Scala and Java run on JVM. So their code must be compiled into bytecode before running on JVM. But Scala compiler supports an optimization technique called tail call recursion. The optimization makes the Scala code compile faster than Java code.


1 Answers

There are two aspects to the (lack of) speed for the Scala compiler.

  1. Greater startup overhead

    • Scalac itself consists of a LOT of classes which have to be loaded and jit-compiled

    • Scalac has to search the classpath for all root packages and files. Depending on the size of your classpath this can take one to three extra seconds.

    Overall, expect a startup overhead of scalac of 4-8 seconds, longer if you run it the first time so disk-caches are not filled.

    Scala's answer to startup overhead is to either use fsc or to do continuous building with sbt. IntelliJ needs to be configured to use either option, otherwise its overhead even for small files is unreasonably large.

  2. Slower compilation speed. Scalac manages about 500 up to 1000 lines/sec. Javac manages about 10 times that. There are several reasons for this.

    • Type inference is costly, in particular if it involves implicit search.

    • Scalac has to do type checking twice; once according to Scala's rules and a second time after erasure according to Java's rules.

    • Besides type checking there are about 15 transformation steps to go from Scala to Java, which all take time.

    • Scala typically generates many more classes per given file size than Java, in particular if functional idioms are heavily used. Bytecode generation and class writing takes time.

    On the other hand, a 1000 line Scala program might correspond to a 2-3K line Java program, so some of the slower speed when counted in lines per second has to balanced against more functionality per line.

    We are working on speed improvements (for instance by generating class files in parallel), but one cannot expect miracles on this front. Scalac will never be as fast as javac. I believe the solution will lie in compile servers like fsc in conjunction with good dependency analysis so that only the minimal set of files has to be recompiled. We are working on that, too.

like image 193
Martin Odersky Avatar answered Sep 28 '22 16:09

Martin Odersky