Clojure is slower than Java and Scala

On performance and Java interoperability: Clojure vs. Scala


I've read several reviews of Clojure vs. Scala, though I realize that both have their place. There are a few considerations that I didn't get a full explanation of when comparing Clojure and Scala:

1.) Which of the two languages ​​is general more quickly ? I understand this will vary from feature to feature, but a general assessment of performance would be helpful. For example: I know that Python dictionaries are very fast. Overall, however, it is one much slower language than Java. I don't want to go with Clojure and run into this problem later.

2.) How is the interoperability with Java? All I've read so far is that Scala has native collection types that make it a bit cumbersome to integrate into a large Java code base, while Clojure has a simple Iterable / Iterator-centric way of interacting with Java classes. More thoughts / details on that?

If it's tight enough between Clojure and Scala, I could end up trying both. One thing about Clojure is that the language very seems easy. On the other hand, Scala has a very flexible type system. But I know Scala is fast (based on multiple personal accounts). So, if Clojure is significantly slower: I'd rather know sooner than later.


Reply:


I think any 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 JIT compiled (except on mobile devices *) while Python is interpreted. Just because they both use a bytecode doesn't mean the implementations are even remotely comparable in performance. But both Scala and Clojure are JVM languages, so they should have similar performance.

Scala has some implementation advantages over Clojure and I would expect slightly higher performance. Although Scala's static typing would normally result in a speed advantage over Clojure's duck typing, does Clojure this supports type hints, which can speed up your code significantly. Ordinary Scala may be faster than ordinary Clojure, but all you need to do is tweak the bottlenecks. Most of a program's runtime is generated by a small portion of the actual code.

In terms of interop with Java, Scala is closer to Java, but I'm sure both languages ​​work well together. In Programming Clojure writes Stuart Halloway: "[You can go on everything access], what you can achieve with Java code. "

And there Scala author Martin Odersky wrote Sun's Java compiler, I kind of believe no bullets were dropped on the Scala side, either. :-)

You would have a hard time picking two better languages, although I like Ruby too. Why do you worry which one to try? Why don't you try both? Scala is more like "the next Java", while it is hard to imagine that Lisp won't start after more than 50 years. But it's clear that Lisp is on its own unique level of abstraction and Clojure is pretty simple, so Scala + Clojure won't be much harder than just (the rather complex) Scala, and I'm sure you won't regret it.

And besides, they work together ...

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







With the current JVM, Scala has an advantage of static typing because the JVM support for dynamic typing - reflection - is slow. In fact, it is precisely for this reason that warnings are often given about a Scala function, which must be implemented using the same techniques, structure types.

Also, Scala accepts mutable objects perfectly, and some algorithms can only be implemented faster with mutability.

Since both Scala and Java are essentially class-based languages, they can work together more easily. Or maybe more seamless. A Java class is a class for Scala and a Scala class is a class for Java. Problems can arise when it comes to Scala's singletons or Java's static members, especially when there is a framework where things are expected to work in a certain way.

So I would go with Scala on both this Accounts. Clojure is a better one in many ways language , and it certainly has very interesting features that Scala does not (yet) have, but you get these benefits when you are fully functional. If that's what you plan to do, then Clojure is very likely to be better. If you don't, then you should probably stick with Scala.







Note that Clojure and Scala are two completely different types of programming languages ​​- Clojure is a functional Lisp-like language that Not is object-oriented. Scala is an object-oriented language with functional programming functions.

In my opinion, the characteristics and concepts of a language (functional, OO, ...) are much more important criteria for choosing a language than its performance (a specific implementation of that language) - although I understand that you don't want me to be in be caught in a language for which no powerful implementation is available.

I would go with Scala because it is object-oriented but also allows you to learn functional programming (if that's what you're interested in). On the other hand, if you're not interested in OO and want to learn "pure" functional programming, try Clojure.







  1. Idiomatic Scala is faster than idiomatic Clojure, and it will remain so.
  2. Both Scala and Clojure sit on Java without any problems. Nobody sits well under it.

If your code is consistently time or space sensitive, stick with Java. But it is not so, even if you think it is.

The Computer languages ​​benchmark game there little insight into Clojure's actual cost of resources. No Clojure data structures are used. Function and sequence abstractions are not displayed.

Clojure may seem simple. It's not, but it's expressive. It may run five times slower than Java, however the source is five times smaller (YMMV). For most applications, this is a great asset. But for some, and for some parts of many others, it is a devastating loss.

Based on experience with the Clojure language, I believe it is possible to say in advance whether your problem will be neatly broken down into a part that can be expressed briefly and appropriately (in terms of performance) in Clojure, and a part that must be run in Java.

  • You can choose Scala Lite: write Java phrases in Scala. You get a certain brevity, a syntax that is easy on the eye, and a coherent, albeit complex, type system.
  • Clojure lite does not exist: There is no point in writing Java idioms in Clojure. All you get is slow java that is difficult to understand as it cuts through the idioms used to express it.

Scala should Got Java right . Clojure is nothing like Java. You could say it Lisp is done right - a bold, some would say absurd claim - that might turn out to be true.



The Computer Language Benchmark Game stats are about the best you are likely to find.

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

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

The statistics say Scala is damn fast.





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

Calling Java from Scala is trivially easy.

Calling Scala from Java is easy as long as you tailor 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 they are not the same. Scala classes can be compiled into a number of classes with names that look funny in Java.

You won't want to mix and match a lot. Creating a component in Scala or Clojure that uses many Java libraries is very doable. You can of course call this component from Java, but you don't want to try using a Scala API that Scala programs are supposed to use from Java.

SVN claims to be "CVS done right". From my point of view, Scala Java is done right.





The November 2010 issue of PragPub discusses the interoperability between Clojure and Java. Calling Java methods is straightforward, but extending Java classes / interfaces is very different.

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

Calling Java code and extending Java classes / interfaces works the same as calling Scala code. Some weaknesses could be some marginal cases when dealing with Java's generics, since the Scala type system is much stronger than that of Java. Creating getters and setters according to the Java Bean convention requires a comment.

Calling Scala from Java is mostly straightforward, but for example Scala companion objects need to know how to compile them into bytecode. Using features with non-abstract methods from Java should also be complicated, and calling methods with special characters would require knowing how they are encoded in the bytecode.


It's now worth checking out (as of May 2010) 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).

To the best of my knowledge, you can use these features when you need them to get speeds that are equivalent to writing the exact same code in pure Java.


We use cookies and other tracking technologies to improve your browsing experience on our website, to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from.

By continuing, you consent to our use of cookies and other tracking technologies and affirm you're at least 16 years old or have consent from a parent or guardian.

You can read details in our Cookie policy and Privacy policy.