Hi,

Usually interpolations for mutlimedia programming are done with

floats, but I'm trying to make it with ints in order to make it

deterministic and also it's maybe faster for the RPi.

So I'm trying to see how I could avoid the calculus that are done several times.

Either with partial application, creating a closure, or even keeping

the intermediate calculus (v_diff, t_diff) the stress test shows that

the simplest (naive?) one is the fastest.

Is there a way to optimise this?

$ cat interpolint.mli

(** Interpolation with ints *)

val inter :

t1:int -> t2:int ->

v1:int -> v2:int -> int -> int

(** simple interpolation *)

type inter_func = int -> int

val interp :

t1:int -> t2:int ->

v1:int -> v2:int -> inter_func

(** interpolation with partial application *)

type inter_value

val interv :

t1:int -> t2:int ->

v1:int -> v2:int -> inter_value

val intervf : inter_value -> int -> int

(** interpolation with intermediate calculus done *)

$ cat interpolint.ml

let inter ~t1 ~t2 ~v1 ~v2 t =

((v2 - v1) * (t - t1)) / (t2 - t1) + v1

type inter_func = int -> int

let interp ~t1 ~t2 ~v1 ~v2 =

let v_diff = (v2 - v1)

and t_diff = (t2 - t1) in

function t ->

(v_diff * (t - t1)) / t_diff + v1

type inter_value = int * int * int * int

let interv ~t1 ~t2 ~v1 ~v2 =

let v_diff = (v2 - v1)

and t_diff = (t2 - t1) in

(t1, v1, v_diff, t_diff)

let intervf (t1, v1, v_diff, t_diff) t =

(v_diff * (t - t1)) / t_diff + v1

$ cat stress.ml

open Interpolint

let timeit n f s =

let t0 = Unix.gettimeofday () in

for i = 1 to n do f () done;

let t1 = Unix.gettimeofday () in

Printf.printf "time(%s): %f\n%!" s (t1 -. t0)

let stress0 () =

for t = 1000 to 1200 do

let _ = inter ~t1:1000 ~t2:1200 ~v1:2 ~v2:8 t in

()

done

let stress1 () =

let f = inter ~t1:1000 ~t2:1200 ~v1:2 ~v2:8 in

for t = 1000 to 1200 do

let _ = f t in

()

done

let stress2 () =

let fi = interp ~t1:1000 ~t2:1200 ~v1:2 ~v2:8 in

for t = 1000 to 1200 do

let _ = fi t in

()

done

let stress3 () =

let v = interv ~t1:1000 ~t2:1200 ~v1:2 ~v2:8 in

for t = 1000 to 1200 do

let _ = intervf v t in

()

done

let () =

timeit 100_000 stress0 "stress0";

timeit 100_000 stress1 "stress1";

timeit 100_000 stress2 "stress2";

timeit 100_000 stress3 "stress3";

;;

$ ./stress.opt

time(stress0): 0.267789

time(stress1): 0.320826

time(stress2): 0.287542

time(stress3): 0.287472

--