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
OCaml image blending performance
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-02-06 (23:38)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] OCaml image blending performance
On Wednesday 06 February 2008 20:29:05 Ilmari Heikkinen wrote:
> The C and Caml versions don't produce the same results, but should
> have the same amount of computation (don't take my word for it though,
> I don't know why the results differ.)

You should post working code if you want people to optimize it. I suspect 
you've incorrectly assumed that OCaml has full-width ints in this case 
because it does work here on my 64-bit machine.

> ./
> cblend
> real	0m1.466s
> user	0m1.456s
> sys	0m0.008s
> blend
> real	0m5.463s
> user	0m5.456s
> sys	0m0.012s
> blend2
> real	0m3.423s
> user	0m3.404s
> sys	0m0.012s

On 2.2GHz AMD64, I get:

$ ./


real    0m6.337s
user    0m6.048s
sys     0m0.032s


real    0m14.965s
user    0m14.281s
sys     0m0.096s


real    0m9.639s
user    0m9.333s
sys     0m0.056s

OCaml is not competitive here for two main reasons:

. Full-size integer arithmetic is not very fast in OCaml.
. Abstractions often cost performance in OCaml.

In this case, most of the speed loss can be regained by simply aggressively 
inlining everything, which is exactly what you (ab)used C macros for in the C 

If you want to get more interesting then you can look at metaprogramming in 
OCaml, either by generating OCaml code, using MetaOCaml or JIT compiling 
using LLVM. You should be able to meet or beat C that way. At the very least, 
you can write OCaml programs to take the pain away from writing C programs by 
hand... :-)

Dr Jon D Harrop, Flying Frog Consultancy Ltd.