Sorry, an error occurred while loading the content.

## 13893Re: "ocaml_beginners"::[] interpolation with ints

Expand Messages
• Jun 4 2:25 PM
2013/06/04, Gabriel Scherer wrote:
> You could take as parameters
> ~t1 ~tdiff ~v1 ~vdiff
> but I don't think this will make a big difference (this purely-integer
> calculus is very fast already).
>
> Does this particular function already show high in your profile?

Not really.
with (((v2 - v1) * (t - t1)) / (t2 - t1) + v1) there are 6 operations,
but 4 would be enough ((v_diff * (t - t1)) / t_diff + v1)
so I thought I could easily save a little there without making the
code too much complex.

Also I noticed that replacing /. 2.0 by *. 0.5 saves by twice. This
simple trick makes save a lot on my desktop computer, but there's not
that much difference there on the RPi.

\$ cat >> interpolint.ml

let interg ~t1 ~t2 ~v1 ~v2 =
let v_diff = (v2 - v1)
and t_diff = (t2 - t1) in
(v_diff, t_diff)

let intervg ~t1 ~t_diff ~v1 ~v_diff t =
(v_diff * (t - t1)) / t_diff + v1

\$ cat >> interpolint.mli

val interg :
t1:int -> t2:int ->
v1:int -> v2:int -> int * int

val intervg :
t1:int -> t_diff:int ->
v1:int -> v_diff:int -> int -> int
(** interpolation with intermediate calculus done,
not given as tuple *)

\$ cat >> stress.ml

let stress4 () =
let t1, t2, v1, v2 = (1000, 1200, 2, 8) in
let v_diff, t_diff = interg ~t1 ~t2 ~v1 ~v2 in
for t = 1000 to 1200 do
let _ = intervg
~t1 ~t_diff
~v1 ~v_diff t
in
()
done

\$ ./stress.opt
time(stress0): 0.267789
time(stress1): 0.320826
time(stress2): 0.287542
time(stress3): 0.287472
time(stress4): 0.293996

--
• Show all 4 messages in this topic