Benefits of OCaml

Comparison with other languages

Verbosity

The amount of code required to perform a given task is an important measure for many reasons. In very verbose languages, the rate at which code can be written can actually be a limiting factor during the development of a program. When maintaining programs, significantly more concise programs are easier to comprehend and, therefore, to maintain. Thus, brevity is a desirable property for a programming language. However, there can be a trade-off between brevity and clarity.

Among programmers familiar with a small set of related languages (such as C++ and Java), verbosity is often considered unimportant because the difference in code size among similar languages is not very large. However, for certain applications, the difference in verbosity between languages can be orders of magnitude. The most obvious example is the use of the ML family of languages in compiler writing compared to C++ or Java, where the difference in the amount of code required to write a compiler can be over an order of magnitude.

OCaml programs are almost always much shorter than programs written in C, C++, Java, C#, Lisp and Scheme. SML is one of the few languages that is comparable to OCaml in terms of verbosity.

Performance

The run-time performance of OCaml programs is now widely known to be very good for a functional language, and is often said to be competitive with C and C++.

However, this view applies only to very small programs, where it is feasible to write equivalent implementations in OCaml and C++. When extrapolated to more substantial programs, this view fails to take into account the huge improvement in development time offered by OCaml. Compared to C++, we find OCaml to be roughly an order of magnitude (10x) faster to develop in. For non-trivial applications, where the optimal approach is not well known, OCaml's faster development time allows programs to be optimised much more effectively. In practice, our OCaml programs are typically many times faster than C++ programs developed in the same amount of time. In one case, one of our OCaml programs was two orders of magnitude faster than a C++ implementation that took longer to develop.

As well as comparing OCaml to conventional languages like C++ and Java, it is interesting to compare OCaml to similarly expressive languages like SML, Haskell, Lisp and Scheme.

Simple OCaml programs are typically slower than MLton-compiled SML and Stalin-compiled Scheme programs but faster than SML/NJ-compiled SML, GHC-compiled Haskell and CMUCL- or SBCL-compiled Lisp. OCaml is very competitive with MLton-compiled SML for floating-point intensive programs, particularly on AMD64, but is significantly (2-5x) slower for symbolic computations.

Feature comparison

A simple table comparing some features of programming languages is instructive when comparing them:

OCamlCC++JavaC#LispSMLHaskell
Interactive top-level
Safe
TypingStaticStaticStaticStaticStaticDynamicStaticStatic
Type inference
EvaluationEagerEagerEagerEagerEagerEagerEagerLazy
First-class lexical closures
Exceptions
List literals
Array literals
Tuples (product type)
Variant types (sum type)
Polymorphic variants
Parametric polymorphism
Pattern matching
Object orientation
Modules
Functors
Type safe marshalling
Polymorphic hashing
Garbage collection
Labelled arguments
Optional arguments
Destructors/Finalisers
Macros
Metaprogramming
Concurrency

This table is somewhat misleading because working around the absence of a feature is arbitrarily easy or difficult. For example, the absence of labelled arguments is fairly easy to work around in SML but the absence of closures in C, C++, Java and C# is much more difficult to work around.