Ocaml performance – native code vs byte code

As noted in this post I’ve been exploring OCaml language. OCaml  is a compiled language and can be compiled either to native code (for supported platforms) or to byte code, which runs interpreted in provided run-time environment. I was wondering, what is performance difference between these two target codes.

To get some basic idea I prepared simple benchmark based on some test code, I’ve created as part of my learning exercise –   QuadTree implementation in OCaml.  QuadTree is used to represent a set of 17k plus cities in US.
Benchmark task is to get all cities within 50km from coordinates  -87.6, 41.8.   To compare QuadTree search performance there is also basic ‘brute-force’ algorithm.  Loading of data and Quad tree construction is not included into the benchmark, it’s limited to searching routine only. Benchmark in done on 64 bit Linux on Core i5 @ 2.7GHz notebook. Exactly same tests were run for native and byte code with these results:

Byte code:

Native code:


As expected native code is faster.   Interesting is that actual gain is dependent on type of task –  Quadtree search, which leverages much more Ocaml structures is faster by factor of 8,  while simpler code for brute-force search if faster only by factor of 2. Also interesting is that heap allocations and garbage collections  (all other columns apart of first one) are slightly different for both codes (again preferring native code).

Another test

More recently I’ve been playing with my learning project (distributed map-reduce), which is bit more complex, using Core and Async libraries –  here difference between native and bytecode was 15 secs vs 35 secs (for word count sample) – so native code was more then  2 x faster.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">