Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Kotlin compile faster than Scala? [closed]

Tags:

kotlin

scala

When we read the wikipedia description of the Kotlin programming language, it is stating that:

JetBrains lead Dmitry Jemerov said that most languages did not have the features they were looking for, with the exception of Scala. However, he cited the slow compile time of Scala as an obvious deficiency.[4] One of the stated goals of Kotlin is to compile as quickly as Java.

How did they achieve that goal? And why is Scala compile time so slow that it was unacceptable for the Kotlin creators? Or - in other words - which features of the Scala compiler make it slower than the Kotlin compiler?

like image 995
Michał Urbaniak Avatar asked Jan 05 '16 15:01

Michał Urbaniak


People also ask

Is Kotlin faster than Scala?

Kotlin compiles directly to bytecode, but it does so much more efficiently than Scala does which means it is much more concise and performs better than Scala.

Why does Scala take so long to compile?

scalac's speed is bounded by two factors: It's a rather large program that runs on the JVM. So startup times are not great because one has to (1) start the JVM (2) load scalac into it (3) JIT compile much of it to gain speed.

Is Kotlin better than Scala?

Considering the job posting on these three platforms, amongst Scala vs Kotlin, Scala is the clear winner. 2. Functional Programming, While both these JVM languages are known for the functional programming paradigm, Scala is the winner between Kotlin vs Scala in terms of functional programming.

Does Kotlin compile faster than Java?

Since Gradle is the default for Kotlin, and with its daemon running and incremental compilation turned on, the clash between Kotlin vs Java performance, Kotlin compiles as fast or slightly faster than Java.


1 Answers

Although I think the question is not well suited for Stack Overflow as it will tend to produce primarily opinion based answers, here is one attempt: You have two different languages, especially concerning the type system, and two completely independent implementations of compilers. So to expect them to have the "same" kind of compilation speed is already a fallacy. I have linked in my comment to another question that examines the speed of the Scala compiler. Basically, it depends on many factors, for example the amount of work that the type inferencer and implicit resolution required by a specific code base.

Nevertheless, I ran a very quick example: I compiled some Project Euler solutions in Kotlin and Scala. This gave me for a fresh re-compile of the whole project:

  • 6 seconds in Kotlin (down to 5 seconds in successive re-builds)
  • 10 seconds in Scala (down to 7 seconds in successive re-builds).

Origin of the source code:

  • I took this code for Kotlin, changed a lot of imports because apparently the Kotlin standard library changed in the meantime, in order to make it compile.
  • I took this code for Scala, and converted it into an sbt project with each problem wrapped in an object pXY extends App { ... } and placed it in a package euler.

Then I removed the files for which only one solution existed, ending up with 26 problems. Both projects were compiled with IntelliJ IDEA 15 CE using Rebuild Project.


To give another perspective on this business, I ran wc (word count) on the sources:

// lines words bytes
931  3603 33087 total  // Kotlin
261  1166  6472 total  // Scala

So now you can either argue that the Kotlin compiler needed to process "more source code" or that the Scala code was "more dense" :)

like image 76
0__ Avatar answered Oct 25 '22 02:10

0__