Re: caml (special) light and numerics

Stefan Monnier (stefan.monnier@lia.di.epfl.ch)
Thu, 19 Oct 1995 09:55:28 +0100

Message-Id: <m0t5qlC-0008TYC@liasg9.epfl.ch>
From: "Stefan Monnier" <stefan.monnier@lia.di.epfl.ch>
To: Thorsten Ohl <ohl@crunch.ikp.physik.th-darmstadt.de>
Subject: Re: caml (special) light and numerics
Date: Thu, 19 Oct 1995 09:55:28 +0100

> How can I get around Array.new in
>
> val f : float array -> float array
>
> let f x =
> let n = Array.length x in
> let y = Array.new n 0.0 in
> for i = 0 to n do
> for j = 0 to n do
> y.(i) <- y.(i) +. a.(i).(j) *. x.(j)
> done
> done
>
> The problem is that I cannot modify `x' in place (even if I know that
> I won't need it later).

>From what I understand, you're concerned more about the fact that "new" might
be slow than about the fact that the code will require two arrays in memory at
the same time (hence having a bigger working set).

You should be aware of the fact that heap allocation is very frequent in caml
and is hence made fairly efficient. In the code above, "new" is very unlikely
to represent more than a few pathetic percents of the time spent in the function
(unless "n" is *very* small (like 0 or 1)).

Stefan