Advertisement
Guest User

Untitled

a guest
Jul 16th, 2021
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Test_tree = struct
  2.   module Tree = struct
  3.     type t =
  4.     | Leaf of string
  5.     | Node of (t * t)
  6.  
  7.     let leaf x = Leaf x
  8.     let node a b = Node (a , b)
  9.   end
  10.  
  11.   module Counter = struct
  12.     module Ephemeron_param = struct
  13.       type t = Tree.t
  14.       let equal = (==)
  15.       let hash = Hashtbl.hash
  16.     end
  17.     module E = Ephemeron.K1.Make(Ephemeron_param)
  18.   end
  19.  
  20.   (* Goal is to associate to each tree an incremental ID, without having to add it to the ADT. *)
  21.   let counter = ref 0
  22.   let h : int Counter.E.t = Counter.E.create 10
  23.   let get_i x =
  24.     match Counter.E.find_opt h x with
  25.     | None -> (
  26.       let counter = counter := !counter + 1 ; !counter in
  27.       Counter.E.add h x counter ;
  28.       counter
  29.     )
  30.     | Some x -> x
  31.   let get x =
  32.     Counter.E.find h x
  33.  
  34.   (* Just create a left-comb of size n, annotate each of its sub-element. *)  
  35.   let test_n n =
  36.     let open Tree in
  37.     let lst = ref @@ leaf "lol" in
  38.     for _ = 1 to n do
  39.       lst := node !lst (leaf "r") ;
  40.       ignore @@ get_i !lst ;
  41.     done ;
  42.     ()
  43.  
  44.   (* Time it *)
  45.   let time f =
  46.       let t = Sys.time() in
  47.       f () ;
  48.       Sys.time () -. t
  49.  
  50.   let time_n n = time (fun () -> test_n n)
  51.  
  52.   let rec ( **) x n = match n with
  53.   | 0 -> 1
  54.   | n -> x * (x ** (n - 1))
  55.  
  56.   let test () =
  57.     for i = 1 to 3 do
  58.       Format.printf "time (%d): %f\n" (10 ** i) (time_n (10 ** i))
  59.     done ;
  60.     ()
  61. end
  62.  
  63. let () =
  64.   Test_tree.test () ;
  65.   ()
  66.  
  67. (*
  68. metadata_test alias public/js/metadata/test/run_metadata_test
  69. time (10): 0.000031
  70. time (100): 0.000554
  71. time (1000): 0.047795
  72. time (10000): 2.469515
  73. *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement