On Performance and Java Interoperability: Clojure vs. Scala

前端 未结 8 676
滥情空心
滥情空心 2021-01-30 01:47

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 exp

相关标签:
8条回答
  • 2021-01-30 01:47

    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 :(

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

    The stats do say that Scala is pretty damn quick.

    0 讨论(0)
  • 2021-01-30 01:48

    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.

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

    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.

    So I'd go with Scala on both these accounts. 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.

    0 讨论(0)
  • 2021-01-30 01:48

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

    It is trivially easy to call Java from Scala.

    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.

    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.

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

    0 讨论(0)
  • 2021-01-30 01:55
    1. Idiomatic Scala is faster than idiomatic Clojure, and will remain so.
    2. Both Scala and Clojure sit easily on top of Java. Neither sits well underneath it.

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

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

    Clojure may appear to be simple. It isn't, but it is expressive. It may run five times slower than Java, but the source is 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.

    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.

    • 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 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.

    0 讨论(0)
  • 2021-01-30 02:01

    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.

    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 does support 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.

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

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

    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.

    And for that matter they interoperate...

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

    0 讨论(0)
  • 2021-01-30 02:05

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

    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.

    0 讨论(0)
提交回复
热议问题