对于数字运算和繁重的字符串处理,Scala 比 Java 7 更快吗?

发布于 2024-11-16 19:47:19 字数 228 浏览 5 评论 0原文

假设有两类应用程序:

(1) 密集的数字运算以及数值和数学计算

(2) 密集的字符串正则表达式匹配、xpath 搜索和其他字符串操作,其中字符串主要存储在集合类中。

在这两种情况下,假设客户端每秒访问这些应用程序数千次,甚至并行访问。

因此,如果我可以选择在服务器后端实现应用程序,我可以选择 Java 7 或 Scala。我应该选择哪一个来获得更快的性能并生成更可靠的代码?

Assume there are two class of applications:

(1) Intensive number crunching and numerical and mathematical computations

(2) Intensive string regex expression matching, xpath searching, and other string manipulations where strings are mostly stored in collection classes.

In Both cases assume clients access these applications thousands of times per second or even in parallel.

So if I have the choice to implement the applications in the server backends, I can choose either Java 7 or Scala. Which one should I choose to get faster performance and produce more reliable code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

可是我不能没有你 2024-11-23 19:47:19

Google 最近做了一些您可能会感兴趣的基准测试 - 请参阅此处链接的论文:http://www.readwriteweb.com/hack/2011/06/cpp-go-java-scala-performance-benchmark.php

这篇论文出人意料地不科学,但是你会大致了解可以做什么。特别感兴趣的可能是 VF 节

Daniel Mahler 通过创建一个改进了 Scala 版本
更多功能版本,保留在 Scala Pro 中
目录。这个版本只有270行代码,大约25%
C++版本的,不仅更短,运行时间也更短
提高了约 3 倍。需要注意的是,这个版本
也执行算法改进,因此
无法与其他专业版本直接比较。

我不清楚这个带有算法改进的版本是否包含在他们的速度基准表中(我不这么认为),但它确实表明您可以通过采用更可行的算法改进来实现性能改进在斯卡拉。然而,它对于简单的字符串处理没有多大作用。

一个重要因素是您对这些语言进行编程的能力以及您在优化它们方面的能力。 Java 显然更冗长,但遇到性能“陷阱”的可能性较小。

Google did some benchmarks recently that you might find interesting - see paper linked to here: http://www.readwriteweb.com/hack/2011/06/cpp-go-java-scala-performance-benchmark.php

The paper is surprisingly un-scientific, but you will get a rough feel for what can be done. Of particular interest may be section V.F

Daniel Mahler improved the Scala version by creating a
more functional version, which is kept in the Scala Pro
directories. This version is only 270 lines of code, about 25%
of the C++ version, and not only is it shorter, run-time also
improved by about 3x. It should be noted that this version
performs algorithmic improvements as well, and is therefore
not directly comparable to the other Pro versions.

It's not clear to me whether this version with algorithmic improvements is included in their speed benchmark table (I don't think so), but it does indicate that you may be able to produce performance improvements by adopting algorithmic improvements that are more viable to implement in Scala. It won't do much for simple string processing, however.

A big factor will be how competent you are in programming these languages, and how good you are at optimizing them. Java is obviously more verbose but you're less likely to run into performance "gotchas".

故乡的云 2024-11-23 19:47:19

与 Java 相比,有两点可能使数值计算具有更好的性能:

  • 实际的一点:Scala 使得“令人尴尬的并行”问题的并行计算变得非常容易。 但需要更多的时间和专业知识,因此很可能只会在极少数情况下完成。

  • 技术方面:Scala 可以专门化原始类型的通用数据结构,从而无需装箱/拆箱。 Java 编译器无法做到这一点。

Scala 使用 Java 的 String,因此这里可能的改进非常有限。但在某些情况下,还有其他数据结构(例如绳索)可以提供比字符串更好的性能。

Two points which might enable better performance for numerical computations than in Java:

  • The practical one: Scala makes it extremely easy to enable parallel computation of "embarrassingly parallel" problems. While the same could be done in Java it would require much more time and expertise, making it likely that it will only be done in rare circumstances.

  • The technical one: Scala can specialize generic data structures for primitive types, making boxing/unboxing unnecessary. The Java compiler is not able to do that.

Scala uses Java's String so the amount of possible improvements here is quite limited. But there are other data structures like ropes which provide better performance than String in some cases.

浊酒尽余欢 2024-11-23 19:47:19

根据您的专业知识和努力,我希望您可以在这里或那里获得更好的结果。通常,只要有无限的开发时间和金钱,您就可以改进、改进和改进每种语言的代码。 (想想越来越大的缓存、专门的排序器、预先计算的默认值等等)。

凭借对两种语言的良好理解以及在您所在领域的性能问题方面的一些经验,我预计不会有太大差异,但是您可以通过更适合收集的 scala 方法节省一些时间,并且在正常开发中节省的时间可能是花费在绩效分析和改进上。

Depending on your expertise and effort, I would expect that you can get better results here or there. Normally, with an infinite amount of development time and money, you can improve, improve and improve your code in every language. (Think of bigger and bigger caches, specialised sorters, precomputed defaults and so on).

With a good understanding of both languages and some experience in performance questions of your field, I wouldn't expect much differences, but you could save some time by the more collection friendly scala approach, and the time, saved on normal development, could be spend in performance analysis and improvement.

黎歌 2024-11-23 19:47:19

原则上,对于数字处理应用程序来说,Scala 没有比 Java 更快的理由。

如果我想编写一个严肃的高性能数字处理应用程序,我不会选择 Java 或 Scala 或任何其他 JVM 语言。

根据我自己的经验(当然这只是轶事证据,绝对不能证明在所有情况下都是如此)JVM 并不是最适合大量数字运算的平台。如果原始数字处理速度很重要,您可能会更好地使用更接近“金属”的东西,例如 C++,它允许您使用 Intel SSE 指令并进行其他低级优化,或者使用 GPU如果您的算法适合的话,请使用 CUDA

There is in principle not really a reason why Scala would be faster than Java for number crunching applications.

I would not choose Java or Scala or any other JVM language if I wanted to write a serious high-performance number crunching application.

From my own experience (and ofcourse this is only anecdotal evidence and definitely not proof that this is true in all cases) the JVM is not the best suited platform for heavy number crunching. If raw number crunching speed is important you would probably be better off with something that's more close to the "metal", for example C++, which allows you to for example use Intel SSE instructions and do other low-level optimizations, or use the GPU with CUDA if your algorithm is suitable for that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文