Haskell, Scala, Clojure, what to choose for high performance pattern matching and concurrency

2ndlife picture 2ndlife · Jul 23, 2012 · Viewed 38.9k times · Source

I have started work on FP recently after reading a lot of blogs and posts about advantages of FP for concurrent execution and performance. My need for FP has been largely influenced by the application that I am developing, My application is a state based data injector into another subsystem where timing is very crucial (close to a 2 million transactions per sec). I have a couple of such subsystems which needs to be tested. I am seriously considering using FP for its parallelism and want to take the correct approach, many posts on SO talk about disadvantages and advantages of Scala, Haskell and Clojure wrt language constructs, libraries and JVM support. From a language point of view I am ok to learn any language as long as it will help me achieve the result.

Certain posts favor Haskell for pattern matching and simplicity of language, JVM based FP lang have a big advantage with respect to using existing java libraries. JaneStreet is a big OCAML supporter but I am really not sure about developer support and help forums for OCAML.

If anybody has worked with handling such large data, please share your experience.

Answer

Rex Kerr picture Rex Kerr · Jul 23, 2012

Do you want fast or do you want easy?

If you want fast, you should use C++, even if you're using FP principles to aid in correctness. Since timing is crucial, the support for soft (and hard, if need be) real-time programming will be important. You can decide exactly how and when you have time to recover memory, and spend only as much time as you have on that task.

The three languages you've stated all are ~2-3x slower than near-optimally hand-tuned C++ tends to be, and then only when used in a rather traditional imperative way. They all use garbage collection, which will introduce uncontrolled random delays in your transactions.

Now, that said, it's a lot of work to get this running in bulletproof fashion with C++. Applying FP principles requires considerably more boilerplate (even in C++11), and most libraries are mutable by default. (Edit: Rust is becoming a good alternative, but it is beyond the scope of this answer to describe Rust in sufficient detail.)

Maybe you don't have the time and can afford to scale back on other specifications. If it is not timing but throughput that is crucial, for example, then you probably want Scala over Clojure (see the Computer Languages Benchmark Game, where Scala wins every benchmark as of this writing and has lower code size in almost every case (Edit: CLBG is not helpful in this regard any more, though you may find archives supporting these statements on the Web Archive)); OCaml and Haskell should be chosen for other reasons (similar benchmark scores, but they have different syntax and interoperability and so on).

As far as which system has the best concurrency support, Haskell, Clojure and Scala are all just fine while OCaml is a bit lacking.

This pretty much narrows it down to Haskell and Scala. Do you need to use Java libraries? Scala. Do you need to use C libraries? Probably Haskell. Do you need neither? Then you can choose either on the basis of which one you prefer stylistically without having to worry overly much that you've made your life vastly harder by choosing the wrong one.