Commits

Sébastien Ferré committed 2798b01

CHange in profiling function to get the number of calls as well as the running time.

Comments (0)

Files changed (1)

   | x::l -> match f x with
       | None -> mapfind f l
       | Some y -> y
+
+(* fold on all ordered pairs of a list *)
+let rec fold_pair : ('a -> 'a -> 'b -> 'b) -> 'a list -> 'b -> 'b =
+  fun f l e ->
+    match l with
+    | [] -> e
+    | x1::xs ->
+       List.fold_right
+         (fun x2 res -> f x1 x2 res)
+         xs
+         (fold_pair f xs e)
+
+let rec scramble : 'a list -> 'a list =
+  function
+  | [] -> []
+  | x::l ->
+     let l' = scramble l in
+     if Random.int 2 = 0
+     then x::l'
+     else l'@[x]
+
+let rec scrambles : 'a list -> int -> 'a list =
+  fun l -> function
+  | 0 -> l
+  | n -> scrambles (scramble l) (n-1)
+
+
+(* for profiling *)
+
+let tbl_prof : (string,(int * float)) Hashtbl.t = Hashtbl.create 100
+
+let prof : string -> (unit -> 'a) -> 'a =
+  fun s f ->
+    let t1 = (Unix.times ()).Unix.tms_utime in
+    let y = f () in
+    let t2 = (Unix.times ()).Unix.tms_utime in
+    let n, t = try Hashtbl.find tbl_prof s with Not_found -> 0, 0. in
+    Hashtbl.replace tbl_prof s (n+1, t +. (t2 -. t1));
+    y