Raybench - OCaml (PLC pt.9)

The French National Institute for computer science and applied mathematics (Inria), has developed forww over 20 years the programming language OCaml, whose main emphasis is on expressiveness and safety.

This functional language, has a very sophisticated type system which supports type inference and polymorphism, with automatic memory management.

Although not very known, this language has been in use mainly in the trading industry by big players like Jane Street with huge success, because of it's speed and data safety.

OCaml can run interpreted code and also generate bytecode and native executables. For this test, I'm just trying native code.


OCaml version 4.02.3 was used for tests.

Compilation time

How fast does the compiler generates the native executable.

$ time ocamlopt -inline 9 -unsafe -ccopt -O9 -o ocamlrb ocamlrb.ml

real    0m0.827s  
user    0m0.736s  
sys    0m0.072s  

Compilation time is as fast as compiling C code, with the option of not compiling anything and got straight to execute the interpreted code.

Running Time

$ time ./ocamlrb

real    3m59.597s  
user    3m58.036s  
sys    0m0.432s  

This is a huge surprise, native code runs around two times slower than C, but easily surpasses every other programming language tested up to now, the closest one being JavaScript running on node.js v6.2.1 which took almost 8 minutes to finish the rendering.

Code Metrics

Line count: 105 code, 17 blanks, 122 total.
File size: 4946 bytes.

The OCaml version has less lines of code and a smaller file size when compared to every other implementation up to this point.

General Thoughts

Just to put it in perspective, this codebase is the smallest one, followed by the Elixir implementation (which I'll talk about next week), and also, the one with the smallest number of lines, followed by Lisp. For expressiveness, this language beats them all others, singlehandedly.

While I would have liked something running as fast as C; having a running time around twice as slow, is still very good. We are not talking about an order of magnitude or more here, as happened with Lisp, Lua, and others. So this is very well regarded as really fast in my book.

What can I say, programming in this language was really pleasant, there is no other way I can put it into words. The syntax and data constructs just let you flow through code and algorithms, and you can test your program using the top level interpreter without having to compile every time.

OCaml seems to be the language I've been looking for.


You can follow the development of this project on GitHub: https://github.com/niofis/raybench