forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjvsrecord.ml
72 lines (62 loc) · 1.67 KB
/
objvsrecord.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
let show l = List.map (fun x -> x#show) l
class integer x =
object
method show = ()
method to_string = string_of_int x
end
type h = < show : unit; to_string : string>
class floating x =
object
method show = ()
method to_string = string_of_float x
end
(* records *)
type element = { show : unit -> unit ; to_string : unit -> string }
let wrap_int x = {
show = (fun () -> ()) ;
to_string = (fun () -> string_of_int x)
}
let wrap_float x = {
show = (fun () -> ()) ;
to_string = (fun () -> string_of_float x)
}
(* bench *)
let test_classes () =
let rec build_classes n acc =
if n <= 0 then
acc
else
build_classes
(pred n)
((new floating (float_of_int n) :> h )
:: (new integer n :> h)
:: acc)
in
let t1 = Sys.time () in
let list = build_classes 1000000 [] in
let t2 = Sys.time () in
List.iter (fun x -> x#show; x#show; x#show; x#show; x#show; x#show;x#show; x#show; x#show; x#show; x#show; x#show;) list ;
t2 -. t1, Sys.time () -. t2
let test_records () =
let rec build_records n acc =
if n <= 0 then
acc
else
build_records
(pred n)
((wrap_float (float_of_int n))
:: (wrap_int n)
:: acc)
in
let t1 = Sys.time () in
let list = build_records 1000000 [] in
let t2 = Sys.time () in
List.iter (fun x -> x.show(); x.show(); x.show();x.show(); x.show(); x.show();x.show(); x.show(); x.show();x.show(); x.show(); x.show();) list ;
t2 -. t1, Sys.time () -. t2
let _ =
let tci, tca = test_classes ()
and tri, tra = test_records () in
Printf.printf
"Classes: build = %f, apply = %f\nRecords: build = %f, apply = %f
\n"
tci tca tri tra