Why Ocaml?

Kevin Murphy, 3 December 2002.

In Fall 2002, I started a project which involved a good mix of string processing, simple statistics and some simple data structures like hash tables and trees. A preliminary prototype in matlab was very slow, so I wanted to look for a more suitable language to implement it in. My desiderata are listed below.

The language should

This left (in my mind) Lisp or ML, both of which meet the above desiderata. (Lisp is a functional programming language with imperative features; ML is a strong, statically-typed version of Lisp.) Deciding between Lisp and ML is harder...

ML vs Lisp

Speed of OCaml

The benchmarks above suggests the Ocaml compiler generates the second fastest code of any of the currently available compilers (gcc and the Intel C compilers being first). Given that Ocaml is also a beautiful language to program in, this is pretty compelling. But maybe the benchmarks are unreliable? See eg Ocaml is only fast if used imperatively, Slashdot 14 March 2005. This is possible. However, I found several other favorable reports on Ocaml's performance. e.g., this example, which implements the Sieve of Eratosthenes for computing primes in Ocaml and C. The Ocaml code is faster, even though the C code is well-written.

In addition, I found this quote from Doug McClain, on a detailed comparative study of C++, IDL, Fortran, SML, Ocaml, Dylan, Erlang, Clean, Haskell, Lisp, Mathematica for scientific computing: "And most importantly, the CAML version works, and it works properly every time. I am assured, having monitored its runtime behavior that there are no memory leaks. Furthermore, the quality of code generated by the CAML compiler has been analyzed by the Intel VTune system and it show no pipeline stalls, maximum parallelism between integer and floating point units, and machine assembly code that is as good or better than can be achieved by hand coding."

So I did my own experiment. It involved a lot of simple floating point arithmetic, plus some string matching. I found the following speedups relative to intrepreted matlab 6.1: Ocaml native code compiler: 10 times faster, Ocaml bytecode compiler: 2 times faster, Matlab mcc compiler: 1.4 times faster. (The matlab code has 670 lines of code, the equivalent Ocaml code has 989 lines.)

Ocaml links

Comparison of other languages

Click here for my comparison.

Going back to Matlab...

After making prototypes of my statistical language project in matlab and Ocaml, I found that the Ocaml version was about 10 times faster than matlab. However, Since January 2003, I have gone back to matlab because I have become more involved in computer vision projects, for which matlab is ideal (although Frank Dellaert uses Ocaml for vision), and because my collaborator uses Matlab.