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
Best Answer
The new volatile is as close as a real "variable" (as it is from many other programming languages) as it gets for clojure.
From the announcement:
For instance, you can set/get and update them just like you would do with a variable in C. The only addition (and hence the name) is the
volatile
keyword to the actual java object.This is to prevent the JVM from optimization and makes sure that it reads the memory location every time it is accessed. From the JIRA ticket:
This mean, a
volatile!
can still be accessed by multiple threads (which is necessary for transducers) but it does not allow to be changed by these threads at the same time since it gives you no atomic updates.The semantics of what
volatile
does is very well explained in a java answer:Now let's see why not use
var-set
ortransients
:Volatile vs var-set
Rich Hickey didn't want to give truly mutable variables:
And thus creating
with-local-vars
,var-set
etc.. The problem with these is that they're true vars and the doc string ofvar-set
tells you:This is, of course, not an option for core.async which potentially executes on different threads. They're also much slower because they do all those checks.
Why not use transients
Transients are similar in that they don't allow concurrent access and optimize mutating a data structure. The problem is that transient only work with collection that implement
IEditableCollection
. That is they're simply to avoid expensive intermediate representation of the collection data structures. Also remember that transients are not bashed into place and you still need some memory location to store the actual transient. Volatiles are often used to simply hold a flag or the value of the last element (seepartition-by
for instance)Summary:
Volatile's are nothing else but a wrapper around java's volatile and have thus the exact same semantics. Don't ever share them. Use them only very carefully.