与 Java 相比,Scala 的性能如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2479819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:00:41  来源:igfitidea点击:

How well does Scala perform compared to Java?

performancescala

提问by Teja Kantamneni

The Question actually says it all.

问题实际上说明了一切。

The reason behind this question is I am about to start a small side project and want to do it in Scala. I am learning scala for the past one month and now I am comfortable working with it. The scala compiler itself is pretty slow (unless you use fsc). So how well does it perform on JVM? I previously worked on groovy and I had seen sometimes over performed than java. My Question is how well scala perform on JVM compared to Java. I know scalahas some very good features(FP, dynamic lang, statically typed...) but end of the day we need the performance...

这个问题背后的原因是我即将开始一个小型项目并希望在Scala. 过去一个月我正在学习 Scala,现在我很舒服地使用它。scala 编译器本身非常慢(除非您使用fsc)。那么它在 JVM 上的表现如何呢?我以前在 groovy 上工作过,我看到有时比 java 表现得更好。我的问题是与 Java 相比,Scala 在 JVM 上的表现如何。我知道scala有一些非常好的特性(FP、动态语言、静态类型......)但归根结底我们需要性能......

回答by Rex Kerr

A majority of my work uses Scala as a high-performance language. If one really pays careful attention to performance, Scala is almost always nearly as good as Java (if not equivalent). If one is careless about e.g. object creations, it can be many times worse--as it can in Java if you use a library that is careless about object creations. (In fact, my Scala code is often faster than my Java code because I find it so much easier to make my highly optimized code convenient to use and reuse--but the Java would be as fast or faster if only I had more time and patience.)

我的大部分工作都使用 Scala 作为高性能语言。如果真的非常关注性能,Scala 几乎总是和 Java 一样好(如果不是等价的)。如果一个人对例如对象创建粗心大意,那么情况可能会更糟——就像在 Java 中,如果您使用一个对对象创建粗心大意的库。(事实上​​,我的 Scala 代码通常比我的 Java 代码快,因为我发现让我的高度优化的代码更易于使用和重用——但是如果我有更多的时间和耐心。)

If you want some data that demonstrates that Scala can be basically as fast as Java, check out the results on the Computer Languages Benchmark Game. (Another less useful but still interesting comparison for high-throughput multicore programming is Tim Bray's Wide Finder 2. This is less useful because the algorithm is not defined in advance, so a large portion of the differences boil down to differences in algorithm.)

如果您想要一些数据来证明 Scala 基本上可以和 Java 一样快,请查看计算机语言基准游戏的结果。(对于高吞吐量多核编程,另一个不太有用但仍然有趣的比较是 Tim Bray 的Wide Finder 2。这不太有用,因为算法没有预先定义,所以很大一部分差异归结为算法的差异。)

回答by les2

Scala is compiled down to byte code and is statically typed so many of the same optimizations that can be done for statically typed languages like Java (as opposed to dynamically typed languages like Groovy) can be done. So comparing Groovy to Scala is comparing apples to oranges.

Scala 被编译成字节码并且是静态类型的,因此可以完成许多可以为 Java 等静态类型语言(与 Groovy 等动态类型语言相反)完成的相同优化。因此,将 Groovy 与 Scala 进行比较就是将苹果与橙子进行比较。

Now, Java to Scala comparison:

现在,Java 与 Scala 的比较:

You can expect Scala to be on par with Java in most situations. Scala can be slow if you program in it stupidly, e.g., tones of mix-ins via Traits could provide some overhead that plain Java wouldn't have.

在大多数情况下,您可以期望 Scala 与 Java 不相上下。如果您愚蠢地在 Scala 中编程,它可能会很慢,例如,通过 Traits 的混合音调可能会提供一些普通 Java 不会有的开销。

But ...

但 ...

If the Traits are actually solving a complex problem in good taste, then a solution in plain Java would have to tackle that same complexity. Who's to say the solution you write in Java using your own patterns is going to be more efficient than what you get for free in Scala (remember, the Scala compiler was written by people that are probably a better programmer than you are).

如果 Traits 实际上是在很好地解决一个复杂的问题,那么纯 Java 的解决方案就必须解决同样的复杂性。谁能说您使用自己的模式用 Java 编写的解决方案将比您在 Scala 中免费获得的解决方案更有效(请记住,Scala 编译器是由可能比您更优秀的程序员编写的)。

On the other hand, if you are using language features for no good reason (e.g., Integer objects when plain int primitives will do), your code will be bloated, slow, crap no matter which language you use.

另一方面,如果您无缘无故地使用语言功能(例如,当纯 int 原语可以使用时,则使用 Integer 对象),无论您使用哪种语言,您的代码都会变得臃肿、缓慢、垃圾。

In addition, consider the special class of request-response based applications that interact with a database or other I/O intensive resource. The bottle neck is not going to be the 'new' operator or virtual method invocation overhead - it will almost certainly be the I/O.

此外,请考虑与数据库或其他 I/O 密集型资源交互的特殊类别的基于请求-响应的应用程序。瓶颈不是“新”操作符或虚方法调用开销——它几乎肯定是 I/O。

In summary, performance between Scala and Java is about the same, and shouldn't be the biggest reason you choose one over the other in 99% of cases. Since skilled human labor is more expensive than computer hardware, you are better off choosing the language that you can (or can learn to) program most efficiently in (including your teammates). If Scala lets you write one tenth the code as Java, you might gain 10X the benefit by using it. If Scala slows you down 10 times (because it's too hard to read), stick with Java!

总而言之,Scala 和 Java 之间的性能大致相同,在 99% 的情况下,这不应成为您选择其中之一的最大原因。由于熟练的人工比计算机硬件更昂贵,因此您最好选择您可以(或可以学习)最有效地编程的语言(包括您的队友)。如果 Scala 允许您将代码编写为 Java 的十分之一,那么使用它您可能会获得 10 倍的收益。如果 Scala 使您慢了 10 倍(因为它太难阅读),请坚持使用 Java!

回答by nullspace

I agree with Rex's comments in this post, and I have personal experience to support it. I converted a Processing applet from java to scala, without changing any implementation details, and both applets rendered a frame in ~6ms, with little variation.

我同意 Rex 在这篇文章中的评论,并且我有个人经验来支持它。我将处理小程序从 java 转换为 Scala,没有更改任何实现细节,并且两个小程序都在大约 6 毫秒内渲染了一个帧,几乎没有变化。