March 2008

This blog has moved on and this post is at

Was inspired by Rober Pickering post about a post about Euler problem 14 …

Without changing the algorithm, I ran a simple test to compare F#, ocaml byte code and ocaml native code … and the result is quite surprising, for me at least!

The program do a lot of computation on integer (even 64 bits); I remember (no ref) that ocaml is more efficient for floating point operation that for integer. But anyway, just wanted an idea of how to port this code to Ocaml.

Following is the code in F# as seen on cited post:

let rec seq_length x n =
match x with
| x when x = 0L -> (n + 1)
| x when x = 1L -> seq_length 0L (n + 1)
| x when x%2L=0L ->seq_length (x/2L) (n + 1)
| _ -> seq_length (3L*x + 1L) (n + 1)

let rec loop i imax n =
let n’ = seq_length i 0
let imax, n = if n’ > n then i, n’ else imax, n
if i < 1000000L then loop (i + 1L) imax n else imax print_any (loop 1L 0L 0)[/sourcecode] And here is an Ocaml version: [sourcecode language="csharp"] let ( ** ) = Int64.mul let ( // ) = Int64.div let ( %% ) = Int64.rem let rec seq_length x n = match x with | 0L -> (n + 1)
| 1L -> seq_length 0L (n + 1)
| x when x %% 2L = 0L -> seq_length (x // 2L) (n + 1)
| _ -> seq_length (Int64.succ (3L ** x)) (n + 1)

let rec loop i imax n =
let n’ = seq_length i 0 in
let imax, n = if n’ > n then (i, n’) else (imax, n) in
if i < 1000000L then loop (Int64.succ i) imax n else imax let _ = print_string (Int64.to_string (loop 1L 0L 0))[/sourcecode] Surprisingly (for me!), the compiled F# code is much faster:

  • time mono eul14.exe: 12.979s
  • time ./eul14.byte: 58.145s
  • time ./eul14.native: 30.234s

I’m not sure why Ocaml seems to be slower for this test. Can it be a GC issue?

I was playing with this mini raytracer and modify it to use glcaml instead of glut.

I was delighted to see how the native code is faster than the byte compiled one … of course this is expected but after playing for some time with the byte code I was surprise how fast the result came after the first native compilation 🙂

(below is google graph; time unit is second)

I’ve been writing here some ideas or development status.

But looks like my readers are more oriented towards code examples or solutions … so I’ll try to be more pragmatic in future.

Anyway my code is better than my English (at least I hope so!) so that should improve quality of this blog 😉

As exercise of Ocaml, I wrote a very very simple library for inference using fuzzy logic.

There is only the bare minimum … but it’s often enough anyway ;). If someone is interested I can:

  • write a small doc
  • write a example
  • complete some parts

This is my first project on GitHub.

Quick news about ocamerl:

  • I changed the build system to ocamlbuild (need ocaml 3.10).
  • … and that’s all! can’t find time to clean all the mess.

So anyway, here is the current state of ocamerl:

  • ocaml can register a hidden erlang node (epmd).
  • ocaml node can respond to ping and keep connection up with erlang node (net_kernel process)
  • erlang can send (some) terms to named ocaml process
  • ocaml can send (some) terms to erlang process if it received the pid in a previous message

That’s all for now! Argh!

Amongst things I’d like to change:

  • simplify terms manipulation in ocaml
  • add features (most important initial message from ocaml to erlang)
  • complete redesign of concurrency (using events or JoCaml maybe!)
  • add a minimum of documentation

I need motivation! In fact I have no project which would need ocaml + erlang … for now.