Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
native vs bytecode
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-08-06 (21:21)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] native vs bytecode
On Wed, Aug 06, 2008 at 03:58:22PM -0400, Ben Aurel wrote:
> As I try to acquire more knowledge about Ocaml

Ben, your invitation to ocaml_beginners has now been approved so these
sorts of questions should go to that list.  There are many of the same
people, and we provide lots of help very quickly, but it keeps
beginners questions out of the way of the experts on this list.

> I made a bit of an unpleseant discovery today. I always was
> fascinated by the execution performance of Ocaml. But now I've
> learned, that this is only true for native binaries and I'm a little
> confused now:

OCaml really is very fast, _when_ you want it to be.

As with all programming, avoid premature optimizations, profile your
code to find out where it's spending its time, and rewrite those parts
that take the most time.

I rarely if ever use the bytecode compiler these days, since there is
no significant advantage to using it.  When I really need speed (after
profiling my program with gprof), I do the following steps in order
until the program is fast enough:

(1) Try to use more suitable data structures (eg. turning assoc-lists
into hash tables or maps).

(2) Rewrite critical functions to be tail recursive.

(3) Rewrite match expressions so that the common case has its own
match pattern and is written more efficiently.

(4) Look at memory usage of data structures and try to minimize.

(5) Turn lists into arrays and rewrite functional stuff using

(6) Turn to C (or even assembly) for critical inner functions.

With this strategy, OCaml programs at Red Hat are performing as well
as C code, and about an order of magnitude faster & an order of
magnitude lower memory usage than programs written in Python, which is
the main language used around here.

When you consider the environmental consequences of running many
virtualized machines in a data centre, order of magnitude reductions
in processor and RAM usage are *significant* (ie. muchos $$$).

> - is it possible to dynamically load native libraries into a native
> program?

Yes, in OCaml 3.11 Dynlink can do this.

> - is it possible to dynamically load bytecode libraries into a
> native program?

Not as far as I'm aware.

> - is it possible to dynamically load native libraries into a
> bytecode program?

Again, I don't think this is possible.

> - is it possible to dynamically load bytecode libraries into a
> bytecode program?

Yes, use the Dynlink module.

> - Are there any performance test that shows how bytecode programs
> stack up agains dynamic languages like ruby, perl and python?

If you look for the "Great Language Shootout" you'll find something.


Richard Jones
Red Hat