Guest User

Untitled

a guest
Feb 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. module Int_field = struct
  2. type t = { mutable x : int; }
  3. let create x = { x }
  4. let set t x = t.x <- x
  5. end
  6.  
  7. module Poly_field = struct
  8. type 'a t = { mutable x : 'a; }
  9. let create x = { x }
  10. let set t x = t.x <- x
  11.  
  12. let set_dynamic t x =
  13. let old_obj = Obj.repr t.x in
  14. let new_obj = Obj.repr x in
  15. if Obj.is_int old_obj && Obj.is_int new_obj then
  16. Array.unsafe_set (Obj.magic t : int array) 0 (Obj.magic x : int)
  17. else if not (old_obj == new_obj) then
  18. t.x <- x
  19. ;;
  20. end
  21.  
  22. let () =
  23. let time_it desc f =
  24. let start = Sys.time () in
  25. f ();
  26. let elapsed = (Sys.time ()) -. start in
  27. Printf.printf "%s took %6f%!\n" desc elapsed;
  28. in
  29.  
  30. let times = 200_000_000 in
  31. time_it "int field" (fun () ->
  32. let t = Int_field.create 0 in
  33. for i = 1 to times do
  34. Int_field.set t i;
  35. done);
  36.  
  37. time_it "poly field" (fun () ->
  38. let t = Poly_field.create 0 in
  39. for i = 1 to times do
  40. Poly_field.set t i;
  41. done);
  42.  
  43. time_it "poly field with dynamic check" (fun () ->
  44. let t = Poly_field.create 0 in
  45. for i = 1 to times do
  46. Poly_field.set_dynamic t i;
  47. done);
  48. ;;
Add Comment
Please, Sign In to add comment