exception Subscript
exception Unequal_length
let hd(xs:'a list):'a = fst((Obj.magic xs):('a * 'b))
let tl(xs:'a list):'a list = snd((Obj.magic xs):('a * 'a list))
let from n =
let rec aux res n = if n = 0 then res else aux (n::res) (n-1)
in aux [] n
let longlist = from 10000
(*
let rec last (xs: 'a list): 'a =
let x :: xs = (Obj.magic xs: 'a list)
in match xs with
x :: [] -> x
| _ :: xs -> last xs
let rec last_safe (xs: 'a list): 'a =
let x, xs = (Obj.magic xs: 'a * 'a list)
in match xs with
x :: [] -> x
| _ :: xs -> last_safe xs
let rec loop_last n =
if n = 0 then () else (last longlist; loop_last (n-1))
let rec loop_last_safe n =
if n = 0 then () else (last_safe longlist; loop_last_safe (n-1))
*)
let rec nth (i, (xs: 'a list)): 'a =
match ((Obj.magic xs): 'a list) with
x :: xs -> if i = 0 then x else nth(i-1, xs)
let rec nth_safe(i, (xs: 'a list)): 'a =
match ((Obj.magic xs): 'a * 'a list) with
(x, xs) -> if i = 0 then x else nth_safe(i-1, xs)
let rec loop_nth n =
if n = 0 then () else (nth(n, longlist); loop_nth(n-1))
let rec loop_nth_safe n =
if n = 0 then () else (nth_safe(n, longlist); loop_nth_safe(n-1))
(*
let rec zip (xs: 'a list) = function
[] -> let [] = xs in []
| y :: ys ->
let x :: xs = (Obj.magic xs: 'a list) in (x, y) :: zip xs ys
let rec zip_safe (xs: 'a list) = function
[] -> []
| y :: ys ->
let x, xs = (Obj.magic xs: 'a * 'a list) in (x, y) :: zip_safe xs ys
let rec loop_zip n =
if n = 0 then ()
else let _ = zip longlist longlist in loop_zip (n-1)
let rec loop_zip_safe n =
if n = 0 then ()
else let _ = zip_safe longlist longlist in loop_zip_safe (n-1)
*)
(*
let rec combine l1 l2 =
match l1 with
[] -> (match l2 with [] -> [])
| a1::l1 -> (match l2 with _ :: _ -> (a1, a1) :: combine l1 l1)
let rec combine1 l1 l2 =
match l1 with
[] -> []
| a1::l1 -> (a1, a1) :: combine1 l1 l1
*)
let test times code arg =
let start = Sys.time () in
let _ = for i = 1 to times do code arg done in
let finish = Sys.time () in
let sys_time = finish -. start
in Printf.printf ("The sys time is %f.\n") sys_time
let compare n code code' arg =
let rec aux code code' sum sum' n =
if n = 0 then (sum, sum')
else
let ct_st = Sys.time () in
let _ = code(arg) in
let ct_fi = Sys.time () in
let ct_st' = Sys.time () in
let _ = code'(arg) in
let ct_fi' = Sys.time () in
aux code code' (sum +. ct_fi -. ct_st) (sum' +. ct_fi' -. ct_st') (n-1) in
let (sum1, sum1') = aux code code' 0.0 0.0 n in
let (sum2', sum2) = aux code' code 0.0 0.0 n
in (sum1 +. sum2, sum1' +. sum2')
(*
let _ =
let (t, t_safe) = compare 10 loop_nth loop_nth_safe 9999
in Printf.printf ("t = %f / t_safe = %f\n") t t_safe
;;
let _ =
let (t_safe, t) = compare 10 loop_nth_safe loop_nth 9999
in Printf.printf ("t = %f / t_safe = %f\n") t t_safe
;;
*)
(*
let _ =
let (t, t_safe) = compare 5 loop_last loop_last_safe 10000
in Printf.printf ("t = %f / t_safe = %f\n") t t_safe
;;
let _ =
let (t_safe, t) = compare 5 loop_last_safe loop_last 10000
in Printf.printf ("t = %f / t_safe = %f\n") t t_safe
;;
let _ =
let (t, t_safe) = compare 10 loop_zip loop_zip_safe 100
in Printf.printf ("t = %f / t_safe = %f\n") t t_safe
;;
let _ =
let (t_safe, t) = compare 10 loop_zip_safe loop_zip 100
in Printf.printf ("t = %f / t_safe = %f\n") t t_safe
;;
*)
let experiment =
let _ =
let (t, t_safe) = compare 10 loop_nth loop_nth_safe 9999
in Printf.printf ("t = %f / t_safe = %f\n") t t_safe
and _ =
let (t_safe, t) = compare 10 loop_nth_safe loop_nth 9999
in Printf.printf ("t = %f / t_safe = %f\n") t t_safe
in ()
;;