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
[Caml-list] O'Caml vs C++: a little benchmark
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Markus Mottl <markus@o...>
Subject: Re: [Caml-list] O'Caml vs C++: a little benchmark
On Sun, 18 Aug 2002, Oleg wrote:
> Firstly, I expected iteration over O'Caml lists and integer arrays to
> be as 
> fast as iteration over std::list and std::vector<int>, respectively.
> Instead, > the benchmark gave me a speed difference of about 10x and
> 100x in favor of 
> C++ for lists and arrays, respectively.

My timings differ considerably (AMD Athlon 800 MHz 256 MB RAM; g++-2.96; instead of


Note, btw., that I have measured user time: real time, which you have
chosen is just too unstable on my machine. Maybe this explains some of
your extreme measurements. I have also used a different file for reversal
(/etc/termcap) and have not run things with your root-script.

OCaml never inlines and/or unrolls recursive functions, giving iterative
solutions in C++ a significant edge concerning optimizations. The same
is true for the array solution, where you are even using higher-order
functions with "fold_left".

> Secondly, a little benchmark comparing mutable binary trees of 64 bit
> floats also showed g++-3.2 to be about an order of magnitude faster.

Not on my machine / with my compiler. Btw., not very fair of you to 
compare ephemeral and persistent datastructures... ;-)

> What was even more surprising was that O'Caml turned out to be about
> 10 times > faster than C++ for reversing lines in a file. I did not
> use explicit buffers > of any kind in either version, and in C++
> program, I used "getline", reading 
> into std::string which should provide about the same level of
> abstraction and > overflow protection as O'Caml string.

Because the C++-iostream library just sucks (at least the implementation 
used by g++). OCaml-I/O as provided by the Pervasives-module is way 

> I'm curious as to where these huge differences for these small
> programs come from.

Look at the assembler output for details... ;-)

Markus Mottl

Markus Mottl                                   
Austrian Research Institute
for Artificial Intelligence        
To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: