为什么 Kotlin 编译速度比 Scala 快?

Why does Kotlin compile faster than Scala?

当我们阅读 Kotlin 编程语言的 wikipedia 描述时,它指出:

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.

他们是如何实现这个目标的?为什么 Scala 编译时间如此之慢以至于 Kotlin 的创造者无法接受?或者 - 换句话说 - Scala 编译器的哪些特性使其比 Kotlin 编译器慢?

尽管我认为这个问题不太适合 Stack Overflow,因为它往往会产生主要基于意见的答案,但这是一种尝试:您有两种不同的语言,尤其是在类型系统方面,以及两个完全独立的实现编译器。所以指望它们有 "same" 那种编译速度已经是谬论了。我在我的评论中链接到另一个检查 Scala 编译器速度的问题。基本上,它取决于许多因素,例如特定代码库所需的类型推断器和隐式解析所需的工作量。

尽管如此,我 运行 举了一个非常简单的例子:我在 Kotlin 和 Scala 中编译了一些 Project Euler 解决方案。这给了我一个全新的 re-compile 整个项目:

  • Kotlin 中的 6 秒(在连续 re-builds 中下降到 5 秒
  • Scala 中为 10 秒(在连续 re-builds 中下降到 7 秒)。

源代码来源:

  • 我采用了 this code for Kotlin,更改了很多导入,因为显然 Kotlin 标准库在此期间发生了变化,以使其能够编译。
  • 我把 this code for Scala 转换成一个 sbt 项目,每个问题都包含在 object pXY extends App { ... } 中,并将其放在包 euler.

然后我删除了只有一个解决方案的文件,最后有 26 个问题。这两个项目都是使用 Rebuild Project.

使用 IntelliJ IDEA 15 CE 编译的

为了从另一个角度看待这项业务,我 运行 wc(字数)来源:

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

所以现在你可以争论 Kotlin 编译器需要处理 "more source code" 或者 Scala 代码是 "more dense" :)