Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Test_tree = struct
- module Tree = struct
- type t =
- | Leaf of string
- | Node of (t * t)
- let leaf x = Leaf x
- let node a b = Node (a , b)
- end
- module Counter = struct
- module Ephemeron_param = struct
- type t = Tree.t
- let equal = (=)
- let hash = Hashtbl.hash
- end
- module E = Ephemeron.K1.Make(Ephemeron_param)
- end
- (* Goal is to associate to each tree an incremental ID, without having to add it to the ADT. *)
- let counter = ref 0
- let h : int Counter.E.t = Counter.E.create 10
- let get_i x =
- match Counter.E.find_opt h x with
- | None -> (
- let counter = counter := !counter + 1 ; !counter in
- Counter.E.add h x counter ;
- counter
- )
- | Some x -> x
- let get x =
- Counter.E.find h x
- (* Just create a left-comb of size n, annotate each of its sub-element. *)
- let test_n n =
- let open Tree in
- let lst = ref @@ leaf "lol" in
- for _ = 1 to n do
- lst := node !lst (leaf "r") ;
- ignore @@ get_i !lst ;
- done ;
- ()
- (* Time it *)
- let time f =
- let t = Sys.time() in
- f () ;
- Sys.time () -. t
- let time_n n = time (fun () -> test_n n)
- let rec ( **) x n = match n with
- | 0 -> 1
- | n -> x * (x ** (n - 1))
- let test () =
- for i = 1 to 3 do
- Format.printf "time (%d): %f\n" (10 ** i) (time_n (10 ** i))
- done ;
- ()
- end
- let () =
- Test_tree.test () ;
- ()
- (*
- metadata_test alias public/js/metadata/test/run_metadata_test
- time (10): 0.000090
- time (100): 0.016346
- time (1000): 4.446515
- *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement