[Shootout-list] Re: [Caml-list] Ocaml sums the harmonic series -- four ways, four benchmarks: floating point performance

Yaron Minsky yminsky@cs.cornell.edu
Sat, 15 Jan 2005 12:13:21 -0500


On Sat, 15 Jan 2005 12:55:19 +0100, Xavier Leroy <Xavier.Leroy@inria.fr> wrote:

> The following slight modification of your code generates asm code that
> is closest to what a C compiler would produce:
> 
> let sum_harmonic5 n =
>   let sum = ref 1.0 in
>   let ifloat = ref 2.0 in
>     for i = 2 to n do
>       sum := !sum +. 1.0/.(!ifloat);
>       ifloat := !ifloat +. 1.0
>     done;
>     !sum +. 0.0;;
> 
> The + 0.0 at the end is ugly but convinces ocamlopt that !sum is best
> kept unboxed during the loop.

That last comment is very interesting and surprising to me.  I've
looked over the optimization suggestions for the compiler, and I don't
understand why that last +. convinces the compiler to unbox sum.  Can
you explain why that is?  Floating point performance is important to
me, and I'd like to get a better grasp on it.

(As a general matter, it would be nice to have some tools to
understand things like unboxing and inlining a little better.  For
example, it would be great to have something akin to -dtypes that
outputs information with which one could check whether a certain
function call is inlined, or whether a certain float is unboxed.)

y