对于数字运算和繁重的字符串处理,Scala 比 Java 7 更快吗?
假设有两类应用程序:
(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Google 最近做了一些您可能会感兴趣的基准测试 - 请参阅此处链接的论文:http://www.readwriteweb.com/hack/2011/06/cpp-go-java-scala-performance-benchmark.php
这篇论文出人意料地不科学,但是你会大致了解可以做什么。特别感兴趣的可能是 VF 节
我不清楚这个带有算法改进的版本是否包含在他们的速度基准表中(我不这么认为),但它确实表明您可以通过采用更可行的算法改进来实现性能改进在斯卡拉。然而,它对于简单的字符串处理没有多大作用。
一个重要因素是您对这些语言进行编程的能力以及您在优化它们方面的能力。 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
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".
与 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.
根据您的专业知识和努力,我希望您可以在这里或那里获得更好的结果。通常,只要有无限的开发时间和金钱,您就可以改进、改进和改进每种语言的代码。 (想想越来越大的缓存、专门的排序器、预先计算的默认值等等)。
凭借对两种语言的良好理解以及在您所在领域的性能问题方面的一些经验,我预计不会有太大差异,但是您可以通过更适合收集的 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.
原则上,对于数字处理应用程序来说,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.