关于性能和 Java 互操作性:Clojure 与 Scala

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1506728/
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-08-12 13:35:50  来源:igfitidea点击:

On Performance and Java Interoperability: Clojure vs. Scala

javaperformancescalaclojurejvm

提问by Ryan Delucchi

I have already read various accounts of Clojure vs. Scala and while I realize that both have their place. There are a few considerations that I haven't acquired a complete explanation on when it comes to comparing both Clojure with Scala:

我已经阅读了 Clojure 与 Scala 的各种描述,虽然我意识到两者都有其一席之地。在比较 Clojure 和 Scala 时,有一些考虑因素我还没有得到完整的解释:

1.) Which of the two languages is generally faster? I realize that this will vary from one language feature to another but an general assessment of performance would be helpful. For example: I know that Python dictionaries are really fast. But as a whole, it is a muchslower language than Java. I don't want to go with Clojure and run into this problem down the road.

1.) 两种语言中哪一种通常更快?我意识到这会因一种语言功能而异,但对性能进行一般评估会有所帮助。例如:我知道 Python 字典非常快。但作为一个整体,它是一个比Java慢语。我不想使用 Clojure 并在路上遇到这个问题。

2.) How is interoperability with Java? All I have read so far is that Scala has native collections types that make it a bit clumsy to integrate with a large Java code-base, whereas Clojure follows a simple Iterable/Iterator-centric way to inter-operate with Java classes. Any more thoughts/details on this?

2.) 与 Java 的互操作性如何?到目前为止,我所读到的只是 Scala 具有本机集合类型,这使得与大型 Java 代码库集成有点笨拙,而 Clojure 遵循简单的以迭代器/迭代器为中心的方式与 Java 类进行互操作。对此有更多想法/细节吗?

Ultimately, if it is a close enough draw between clojure and scala, I might try them both. One thing about Clojure is the language seems verysimple. But then again, Scala has a very flexible type system. But, I know that Scala is fast (based on multiple personal accounts). So, if Clojure is significantly slower: I'd like to know sooner rather than later.

最终,如果 clojure 和 scala 之间足够接近,我可能会同时尝试它们。关于 Clojure 的一件事是语言看起来非常简单。但话说回来,Scala 有一个非常灵活的类型系统。但是,我知道 Scala 很快(基于多个个人帐户)。所以,如果 Clojure 明显慢得多:我想早点知道。

采纳答案by DigitalRoss

I think either language will be fast enough for you. When comparing Python and Java, it seems a bit unreasonable to blame the language for the speed difference. Java is compiled JIT (except on mobile devices*) whereas Python is interpreted. Just because both use a bytecode does not mean the implementations will have even remotely comparable performance. But both Scala and Clojure are JVM languages so they should have similar performance.

我认为任何一种语言对你来说都足够快。在比较 Python 和 Java 时,将速度差异归咎于语言似乎有点不合理。Java 是 JIT 编译的(移动设备除外*),而 Python 是解释性的。仅仅因为两者都使用字节码并不意味着实现将具有甚至远程可比的性能。但是 Scala 和 Clojure 都是 JVM 语言,因此它们应该具有相似的性能。

Scala has a few implementation advantages over Clojure and I would expect somewhat higher performance. Although Scala's static typing would normally translate into a speed advantage over Clojure's duck typing, Clojure doessupport type hinting which can speed up code considerably. Possibly, ordinary Scala is faster than ordinary Clojure, but you only need to optimize the bottlenecks. Most of a program's run time is generated by a small amount of the actual code.

与 Clojure 相比,Scala 有一些实现优势,我希望性能会更高一些。尽管 Scala 的静态类型通常会转化为优于 Clojure 的鸭子类型的速度优势,但 Clojure确实支持类型提示,这可以显着加快代码速度。可能,普通的Scala比普通的Clojure要快,但你只需要优化瓶颈。程序的大部分运行时间是由少量实际代码生成的。

Regarding interop w/ Java, Scala is closer to Java but I'm sure both languages interoperate well. In Programming ClojureStuart Halloway writes: "[you can access] anything you could reach from Java code.".

关于与 Java 的互操作,Scala 更接近 Java,但我确信两种语言都能很好地互操作。在编程 Clojure 中,Stuart Halloway 写道:“[你可以访问]任何你可以从 Java 代码中获得的东西。”。

And since Scala author Martin Odersky wroteSun's Java compiler, I kinda think no balls have been dropped on the Scala side, either. :-)

自从 Scala 的作者 Martin Odersky编写了Sun 的 Java 编译器,我觉得 Scala 方面也没有任何问题。:-)

You would be hard-pressed to pick two better languages, though I like Ruby also. Why are you worried about which one to try? Why not try them both? Scala is more likely to be "the next Java", while it's hard to imagine that Lisp will finally take off after not doing so for over 50 years. But it's clear that Lisp is on its own unique level of abstraction, and Clojure is fairly simple, so Scala + Clojure won't be that much harder than just (the rather complex) Scala and I'm sure you will be glad you did it.

你很难选择两种更好的语言,尽管我也喜欢 Ruby。你为什么担心尝试哪一个?为什么不试试它们呢?Scala 更有可能成为“下一个 Java”,而很难想象 Lisp 会在 50 多年不这样做后最终腾飞。但很明显,Lisp 处于自己独特的抽象级别,而 Clojure 相当简单,因此 Scala + Clojure 不会比(相当复杂的)Scala 难多少,我相信你会很高兴你做到了它。

And for that matter they interoperate...

就此而言,它们是互操作的……

* dalvik (android's JVM) got a JIT compiler in 2.2 version in 2010

* dalvik(android 的 JVM)在 2010 年获得了 2.2 版本的 JIT 编译器

回答by Bill K

The stats produced by the "Computer Language Benchmark Game" are about the best you're probably going to find.

计算机语言基准游戏”产生的统计数据是您可能会找到的最好的统计数据。

They are in-depth and you can compare many languages. The problem is that they don't cover Clojure :(

它们很深入,您可以比较多种语言。问题是它们不包括 Clojure :(

That said, it's pretty easy to submit anything--it's all open source.

也就是说,提交任何东西都很容易——它都是开源的。

The stats do say that Scala is pretty damn quick.

统计数据确实表明 Scala 非常快。

回答by Daniel C. Sobral

With the present JVM Scala has an advantage on the account of being statically typed, as JVM support for dynamic typing -- reflection -- is slow. In fact, one Scala feature which must be implemented through the same techniques, structural types, is often warned against for this very reason.

对于当前的 JVM,Scala 在静态类型方面具有优势,因为 JVM 对动态类型(反射)的支持很慢。事实上,一个必须通过相同技术实现的 Scala 特性,结构类型,经常因为这个原因而受到警告。

Also, Scala accepts mutable objects just fine, and some algorithms are just faster to implement with mutability.

此外,Scala 可以很好地接受可变对象,并且某些算法在可变性方面的实现速度更快。

As both Scala and Java are essentially class-based languages, they interoperate more easily. Or, perhaps, more seamlessly. A Java class is a class to Scala, and a Scala class is a class to Java. Problems might arise when it comes to Scala's singletons or Java's static members, particularly when there's a framework involved expecting things to work in a certain way.

由于 Scala 和 Java 本质上都是基于类的语言,因此它们更容易互操作。或者,也许更无缝。Java 类是 Scala 的类,Scala 类是 Java 的类。当涉及到 Scala 的单例或 Java 的静态成员时,可能会出现问题,特别是当涉及到期望事物以某种方式工作的框架时。

So I'd go with Scala on both theseaccounts. Clojure is, in many ways, a better language, and it certainly has very interesting features not present (so far) on Scala, but you reap such benefits by going fully functional. If you intend to do that, then Clojure is very likely better. If you don't, then you should probably stay with Scala.

所以我会在这两个帐户上使用 Scala 。在很多方面,Clojure 是一种更好的语言,它当然具有 Scala 上(到目前为止)不存在的非常有趣的特性,但是通过实现全功能,您可以获得这样的好处。如果您打算这样做,那么 Clojure 很可能会更好。如果您不这样做,那么您可能应该继续使用 Scala。

回答by Jesper

Note that Clojure and Scala are two totally different types of programming languages - Clojure is a functional Lisp-like language, it is notobject oriented. Scala is an object oriented language which has functional programming features.

请注意,Clojure 和 Scala 是两种完全不同的编程语言 - Clojure 是一种类似函数式 Lisp 的语言,它不是面向对象的。Scala 是一种面向对象的语言,具有函数式编程特性。

In my opinion, the features and concepts of a language (functional, OO, ...) are much more important criteria for choosing a language than the performance (of a particular implementation of that language) - altough I understand that you don't want to get trapped into a language for which there is no well-performing implementation available.

在我看来,语言的特性和概念(函数式、面向对象、...)是选择语言的重要标准,而不是性能(该语言的特定实现)——尽管我知道你不知道想要陷入一种没有性能良好的实现可用的语言中。

I'd go for Scala, because it is object oriented but also allows you to learn functional programming (if you're interested in that). On the other hand, if you don't care about OO and you want to learn "pure" functional programming, try Clojure.

我会选择 Scala,因为它是面向对象的,但也允许您学习函数式编程(如果您对此感兴趣)。另一方面,如果您不关心 OO 并且想学习“纯”函数式编程,请尝试 Clojure。

回答by Kevin Peterson

On interoperability, I can't speak for Clojure, but I would expect it to be in a similar situation as Scala.

关于互操作性,我不能代表 Clojure,但我希望它处于与 Scala 类似的情况。

It is trivially easy to call Java from Scala.

从 Scala 调用 Java 非常容易。

It is easy to call Scala from Java as long as you conform your external API to the common points between Scala and Java. For example, a Scala object is used in some ways like static methods in Java, but it's not the same thing. Scala classes may compile to a number of classes with names that look funny in Java.

只要您使外部 API 符合 Scala 和 Java 之间的共同点,就可以轻松地从 Java 调用 Scala。例如,Scala 对象的使用方式类似于 Java 中的静态方法,但它不是一回事。Scala 类可以编译为许多类,它们的名称在 Java 中看起来很有趣。

You will not want to mix and match much. Building component in Scala or Clojure that uses lots of Java libraries is very feasible. You can of course call into this component from Java, but what you are not going to want to do is try to consume a Scala API intended for use by Scala programs from Java.

你不会想要混搭太多。在 Scala 或 Clojure 中构建使用大量 Java 库的组件是非常可行的。您当然可以从 Java 调用此组件,但您不想做的是尝试使用供 Java Scala 程序使用的 Scala API。

SVN claims to be "CVS done right". In my view, Scala is Java done right.

SVN 声称“CVS 做对了”。在我看来,Scala 是 Java 做的对。

回答by mikera

It's now (as of May 2010) worth loking at the latest 1.2 branch of Clojure - this includes a lot of additional support for primitive types and static typing (through various type hints and protocols).

现在(截至 2010 年 5 月)值得关注 Clojure 的最新 1.2 分支——这包括对原始类型和静态类型的许多额外支持(通过各种类型提示和协议)。

My understanding is that you can use these features when you need it to get speed equivalent to writing exactly the same code in pure Java.

我的理解是,您可以在需要时使用这些功能来获得与用纯 Java 编写完全相同的代码相当的速度。

回答by Esko Luontola

The November 2010 issue of PragPubdiscusses Clojure-Java interoperability. Calling Java methods is straightforward, but extending Java classes/interfaces is quite different.

PragPub的2010年11月问题讨论的Clojure,Java的互操作性。调用 Java 方法很简单,但扩展 Java 类/接口则完全不同。

Scala on the other hand is much closer to Java. Scala-Java interoperability is elaborated at http://www.codecommit.com/blog/java/interop-between-java-and-scala

另一方面,Scala 更接近 Java。Scala-Java 互操作性在http://www.codecommit.com/blog/java/interop-between-java-and-scala详细说明

Calling Java code and extending Java classes/interfaces works the same way as calling Scala code. Some pain points might be some edge cases of dealing with Java's generics, because Scala's type system is much strongerthan Java's. Creating getters and setters following the Java Bean convention requires an annotation.

调用 Java 代码和扩展 Java 类/接口的工作方式与调用 Scala 代码相同。一些痛点可能是处理 Java 泛型的一些边缘情况,因为 Scala 的类型系统比 Java强得多。按照 Java Bean 约定创建 getter 和 setter需要一个注释

Calling Scala from Java is most of the time straightforward, but for example Scala's companion objects requires knowing how they are compiled to bytecode. Also using traits with non-abstract methods from Java should be complicated, and calling methods with special characters would require knowing how they are encoded in the bytecode.

大多数时候从 Java 调用 Scala 很简单,但例如 Scala 的伴生对象需要知道它们是如何编译成字节码的。同样,使用来自 Java 的非抽象方法的特征应该很复杂,并且使用特殊字符调用方法需要知道它们是如何在字节码中编码的。

回答by Thumbnail

  1. IdiomaticScala is faster than idiomaticClojure, and will remain so.
  2. Both Scala and Clojure sit easily on top of Java. Neither sits well underneath it.
  1. 成语斯卡拉快于惯用的Clojure,并将继续如此。
  2. Scala 和 Clojure 都可以轻松地置于 Java 之上。两者都坐在它下面。

If your code is time-critical or space-critical throughout, stick to Java. But it isn't, even if you think it is.

如果您的代码始终对时间或空间要求严格,请坚持使用 Java。但它不是,即使你认为它是。

The Computer Language Benchmark Gamesheds little light on Clojure's true resource costs. No Clojure data structures are employed. Functional and sequence abstractions do not appear.

计算机语言基准游戏揭示了Clojure的真实资源成本小光。没有使用 Clojure 数据结构。不会出现功能和序列抽象。

Clojure may appear to be simple. It isn't, but it is expressive. It may run five times slower than Java, but the sourceis five times smaller (YMMV). For most of most applications, this is a big win. But for some, and for some parts of many others, it's a devastating loss.

Clojure 可能看起来很简单。它不是,但它是富有表现力的。它的运行速度可能比 Java 慢五倍,但源代码要小五倍(YMMV)。对于大多数应用程序来说,这是一个巨大的胜利。但对于某些人,对于其他许多人的某些部分,这是毁灭性的损失。

With experience of the Clojure language, I believe it is possible to tell in advance whether your problem will cleave cleanly into a part that can be succinctly and adequately (in performance terms) expressed in Clojure and a part that needs doing in Java.

凭借 Clojure 语言的经验,我相信可以提前判断您的问题是否会清晰地分为可以用 Clojure 表达的部分(在性能方面)和需要用 Java 进行的部分。

  • You can go for Scala lite: writing Java idioms in Scala. You'll gain some brevity, a syntax that's easier on the eye, and a coherent albeit complex type system.
  • There is no such thing as Clojure lite: writing Java idioms in Clojure is utterly pointless. All you'll get is slow Java that's hard to understand because it cuts across the grain of the idioms used to express it.
  • 您可以选择 Scala lite:在 Scala 中编写 Java 习语。您将获得一些简洁性、更易于理解的语法以及连贯但复杂的类型系统。
  • 没有 Clojure lite 这样的东西:在 Clojure 中编写 Java 习语是完全没有意义的。你将得到的只是缓慢的 Java,它很难理解,因为它跨越了用来表达它的习语。

Scala has been said to be Java done right. Clojure is nothing like Java. You might say that it is Lisp done right- a bold, some would say preposterous, claim - which may turn out to be true.

Scala 一直被认为是Java 做的对。Clojure 与 Java 完全不同。你可能会说Lisp 做得对——一个大胆的,有些人会说荒谬的声明——结果可能是真的。