Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Int_field = struct
- type t = { mutable x : int; }
- let create x = { x }
- let set t x = t.x <- x
- end
- module Poly_field = struct
- type 'a t = { mutable x : 'a; }
- let create x = { x }
- let set t x = t.x <- x
- let set_dynamic t x =
- let old_obj = Obj.repr t.x in
- let new_obj = Obj.repr x in
- if Obj.is_int old_obj && Obj.is_int new_obj then
- Array.unsafe_set (Obj.magic t : int array) 0 (Obj.magic x : int)
- else if not (old_obj == new_obj) then
- t.x <- x
- ;;
- end
- let () =
- let time_it desc f =
- let start = Sys.time () in
- f ();
- let elapsed = (Sys.time ()) -. start in
- Printf.printf "%s took %6f%!\n" desc elapsed;
- in
- let times = 200_000_000 in
- time_it "int field" (fun () ->
- let t = Int_field.create 0 in
- for i = 1 to times do
- Int_field.set t i;
- done);
- time_it "poly field" (fun () ->
- let t = Poly_field.create 0 in
- for i = 1 to times do
- Poly_field.set t i;
- done);
- time_it "poly field with dynamic check" (fun () ->
- let t = Poly_field.create 0 in
- for i = 1 to times do
- Poly_field.set_dynamic t i;
- done);
- ;;
Add Comment
Please, Sign In to add comment